15

在此处输入图像描述

有没有办法改变 swiftUI 中的 tabView 指示器颜色?

这是我的代码

struct OnBoarding: View {
    var body: some View {
        
        TabView {
            ForEach(0 ..< 3) { item in
                VStack {

                    Image("discover")
                        .resizable()
                        .scaledToFit()
                    
                }
            }
        }

        .tabViewStyle(PageIndexViewStyle(backgroundDisplayMode: Color ?))
        
    }
}

struct OnBoarding_Previews: PreviewProvider {
    static var previews: some View {
        OnBoarding()
    }
}

我尝试了 tabViewStyle(PageIndexViewStyle(backgroundDisplayMode: Color ?)) 但无法解决

4

3 回答 3

31

你需要使用 UIkit

 init() {
    UIPageControl.appearance().currentPageIndicatorTintColor = .red
    UIPageControl.appearance().pageIndicatorTintColor = UIColor.black.withAlphaComponent(0.2)
    }
于 2020-07-18T16:33:45.903 回答
13

基本上,您需要在视图出现时设置全局变量。一种方法如下:

import SwiftUI

struct OnboardingView: View {
  
  var pages: [Page]
  
  var body: some View {
  
    TabView {
      ForEach(pages) { page in
        // Your component view
      }
    }
    .tabViewStyle(PageTabViewStyle())
    .onAppear {
      setupAppearance()
    }
  }
  
  func setupAppearance() {
    UIPageControl.appearance().currentPageIndicatorTintColor = .black
    UIPageControl.appearance().pageIndicatorTintColor = UIColor.black.withAlphaComponent(0.2)
  }
}
于 2020-09-16T11:15:10.450 回答
2

首先创建一个子视图,如:

struct SliderTabView: View {
init() {
          UIPageControl.appearance().currentPageIndicatorTintColor = .red
          UIPageControl.appearance().pageIndicatorTintColor = UIColor.red.withAlphaComponent(0.2)
       }
var body: some View {
    TabView{
        ForEach(players){ player in
            //Slider content here ...
        }
    }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
}

从主视图调用子视图,如:

    struct ContentView: View {
          var body: some View {
                                 SliderTabView()
                              }
                             }

输出看起来像我: 在此处输入图像描述

于 2021-06-17T05:21:42.360 回答