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
you need to use UIkit
init() {
UIPageControl.appearance().currentPageIndicatorTintColor = .red
UIPageControl.appearance().pageIndicatorTintColor = UIColor.black.withAlphaComponent(0.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)
}
}
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()
}
}
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
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.