首页 > 解决方案 > TabView“点”索引颜色不变

问题描述

如果您使用.tabViewStyle(PageTabViewStyle(indexDisplayMode: .always)),那么您的标签栏将显示为一组点,指示您当前的页面索引。默认情况下它是白色的,我不能改变它。在下面的代码中,我将展示我围绕这个问题所做的所有尝试(它们都不起作用)。主要问题:如何改变点的颜色?PS:重要通知!在预览中,我看到了这些变化,但是当启动模拟器时,着色消失了。

struct ContentView: View {

var body: some View {
    TabView {
        Text("Home Tab")
            .font(.system(size: 30, weight: .bold, design: .rounded))
            .tabItem {
                Text("Home")
            }
     
        Text("Bookmark Tab")
            .font(.system(size: 30, weight: .bold, design: .rounded))
            .tabItem {
                Text("Bookmark")
            }
    }
    .onAppear(){
        UIPageControl.appearance().currentPageIndicatorTintColor = .blue;
        UITabBar.appearance().tintColor = .red;
        UITabBar.appearance().backgroundColor = UIColor.red
        UITabBar.appearance().barTintColor = .red;
    }
    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .always))
    .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
   }
}

[![ColorImage][1]][1]


  [1]: https://i.stack.imgur.com/P25ST.png

标签: swiftswiftui

解决方案


使用init代替onAppear

struct TestScrollView: View {
    init() {
        UIPageControl.appearance().currentPageIndicatorTintColor = .blue
        UIPageControl.appearance().pageIndicatorTintColor = .red
        UIPageControl.appearance().tintColor = .red
    }
    var body: some View {


推荐阅读