Swift TabViewのインジケーターの色を変えるサンプル
環境
Swift version 5.2.3 (swift-5.2.3-RELEASE)
概要
TabViewのインジケーターの色を変えるには、UIPageControlを使います。
init {
//選択中のインジケータの色を代入
UIPageControl.appearance().currentPageIndicatorTintColor = selectedColor
//非選択中のインジケータの色を代入
UIPageControl.appearance().pageIndicatorTintColor = unselectedColor
}
init {
//選択中のインジケータの色を代入
UIPageControl.appearance().currentPageIndicatorTintColor = selectedColor
//非選択中のインジケータの色を代入
UIPageControl.appearance().pageIndicatorTintColor = unselectedColor
}
init { //選択中のインジケータの色を代入 UIPageControl.appearance().currentPageIndicatorTintColor = selectedColor //非選択中のインジケータの色を代入 UIPageControl.appearance().pageIndicatorTintColor = unselectedColor }
UIPageControl.appearance().currentPageIndicatorTintColorに代入した色は、
選択中のインジケーターの色。
UIPageControl.appearance().pageIndicatorTintColorに代入した色は、
非選択のインジケータの色になります。
使用例
struct ContentView: View {
init() {
UIPageControl.appearance().currentPageIndicatorTintColor = .red
UIPageControl.appearance().pageIndicatorTintColor = UIColor.red.withAlphaComponent(0.2)
}
var body: some View {
VStack {
TabView {
PageView(text: "Home View")
PageView(text: "Data View")
PageView(text: "Test View")
}
.tabViewStyle(PageTabViewStyle())
}
}
}
struct ContentView: View {
init() {
UIPageControl.appearance().currentPageIndicatorTintColor = .red
UIPageControl.appearance().pageIndicatorTintColor = UIColor.red.withAlphaComponent(0.2)
}
var body: some View {
VStack {
TabView {
PageView(text: "Home View")
PageView(text: "Data View")
PageView(text: "Test View")
}
.tabViewStyle(PageTabViewStyle())
}
}
}
struct ContentView: View { init() { UIPageControl.appearance().currentPageIndicatorTintColor = .red UIPageControl.appearance().pageIndicatorTintColor = UIColor.red.withAlphaComponent(0.2) } var body: some View { VStack { TabView { PageView(text: "Home View") PageView(text: "Data View") PageView(text: "Test View") } .tabViewStyle(PageTabViewStyle()) } } }