change TabView indicator SwiftUI
you need to use UIkit
init() {
UIPageControl.appearance().currentPageIndicatorTintColor = .red
UIPageControl.appearance().pageIndicatorTintColor = UIColor.black.withAlphaComponent(0.2)
}
Move Index View above home indicator in Tab View
EDIT: See also @nekno's fantastic additions!
This is possible if you create a custom UIPageControl
, manually tag each tab in the TabView
, and make sure to keep track of the numberOfPages
:
struct PageControlView: UIViewRepresentable {
@Binding var currentPage: Int
@Binding var numberOfPages: Int
func makeUIView(context: Context) -> UIPageControl {
let uiView = UIPageControl()
uiView.backgroundStyle = .prominent
uiView.currentPage = currentPage
uiView.numberOfPages = numberOfPages
return uiView
}
func updateUIView(_ uiView: UIPageControl, context: Context) {
uiView.currentPage = currentPage
uiView.numberOfPages = numberOfPages
}
}
struct ContentView: View {
@State var isSheetUp = false
var body: some View {
Button("Present") {
isSheetUp.toggle()
}
.sheet(isPresented: $isSheetUp) {
Sheet()
}
}
struct Sheet: View {
@State var currentPage = 0
@State var numberOfPages = 3
var body: some View {
NavigationView {
ZStack {
TabView(selection: $currentPage) {
Page().tag(0)
Page().tag(1)
Page().tag(2)
}
// Comment this to switch layout issue
.ignoresSafeArea(edges: .bottom)
.tabViewStyle(.page(indexDisplayMode: .never))
.indexViewStyle(.page(backgroundDisplayMode: .always))
.navigationTitle("Title")
.navigationBarTitleDisplayMode(.inline)
VStack {
Spacer()
PageControlView(currentPage: $currentPage, numberOfPages: $numberOfPages)
}
}
}
}
}
struct Page: View {
var body: some View {
ScrollView {
VStack {
Rectangle()
.foregroundColor(.teal)
.padding()
.frame(minHeight: 10000)
}
}.background(Color.brown)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
TabView dot index color does not change
Use init
instead of onAppear
struct TestScrollView: View {
init() {
UIPageControl.appearance().currentPageIndicatorTintColor = .blue
UIPageControl.appearance().pageIndicatorTintColor = .red
UIPageControl.appearance().tintColor = .red
}
var body: some View {
TabView Indicator doesn't move when page changes
The green area below does move to the right, but only 1 pixel. Try something like this example code, choose the value (200) most suited for your purpose:
.onAppear {
self.tabOffset = CGFloat(index*200) // <-- here
}
Page indicator in tabview swiftui
Add an init() to the View where you define the UIKit appearance.
struct YourView: View {
init() {
UIPageControl.appearance().currentPageIndicatorTintColor = .black
UIPageControl.appearance().pageIndicatorTintColor = .gray
}
var body: some View {
// Text("YourView") with TabView
}
}
Related Topics
In Swift, Does Resetting the Property Inside Didset Trigger Another Didset
Swiftui Navigation on iPad - How to Show Master List
Can't Pass Date to Nspredicate(Format: ...) Without "As Cvararg"
Swift Realm, Load the Pre-Populated Database the Right Way
Lazy Readonly Property in Swift
Resize a Sklabelnode Font Size to Fit
Scenekit - Animation with Dae File Format
Scenekit Shadow on a Transparent Scnfloor()
Why Can You Assign Non-Optional Values to Optional Types in Swift
Clearing Uiwebview's Cache in Swift
Swift Codable Decode Manually Optional Variable
Uitableview - Multiple Selection and Single Selection
How to Make Rounded Corner Progress Bar in Swift
Datepicker Using Time-Interval in Swiftui