'change TabView indicator SwiftUI

enter image description here

Is there a way to change the tabView Indicator color in swiftUI ?

This is my code

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()
    }
}

I tried tabViewStyle(PageIndexViewStyle(backgroundDisplayMode: Color ?)) but can't get around with it



Solution 1:[1]

you need to use UIkit

 init() {
    UIPageControl.appearance().currentPageIndicatorTintColor = .red
    UIPageControl.appearance().pageIndicatorTintColor = UIColor.black.withAlphaComponent(0.2)
    }

Solution 2:[2]

Basically you need to set the global variable when your view appears. One way to do it is as follows:

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)
  }
}

Solution 3:[3]

First create a subview like:

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))
}

Call the subview from main view like:

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

the output look like of me: enter image description here

Solution 4:[4]

If you are using SwiftUI from UIKit there's a more delicate way to update UIPageControl appearance.

// SwiftUI with with UIPageControl
struct MyView: View { ... }
import SwiftUI
import UIKit

final class MyViewContainerController: UIHostingController<MyView> {
    init() {
        let view = MyView()
        super.init(rootView: view)
    }

    @available(*, unavailable)
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let containerTypes = [MyViewContainerController.self]
        let appearance = UIPageControl.appearance(whenContainedInInstancesOf: containerTypes)
        appearance.currentPageIndicatorTintColor = .systemBlue
        appearance.pageIndicatorTintColor = .systemIndigo
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Achraf
Solution 2 Rufat Mirza
Solution 3
Solution 4 podkovyr