Sunday, May 22, 2022
HomeiOS Developmentios - SwiftUI PageTabViewStyle doesn't ignore secure space

ios – SwiftUI PageTabViewStyle doesn’t ignore secure space


I am attempting to implement a PageTabViewStyle inside a NavigationView so that I can swipe between two different lists but the TabView does not ignore the safe area despite having .ignoresSafeArea().

Code

struct TestView: View {
    @State private var selectedPage = 0
    private var pages = [0, 1]
    
    var body: some View {
        NavigationView {
            TabView(selection: $selectedPage) {
                FirstList()
                    .ignoresSafeArea()
                    .tag(0)
                
                SecondList()
                    .tag(1)
            }
            .ignoresSafeArea()
            .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .principal) {
                    Picker("", selection: $selectedPage) {
                        ForEach(pages, id: .self) {
                            Text(String($0))
                        }
                    }
                    .scaledToFit()
                    .pickerStyle(.segmented)
                }
            }
        }
    }
}

struct FirstList: View {
    var body: some View {
        List {
            Text("0")
        }
    }
}

struct SecondList: View {
    var body: some View {
        List {
            Text("1")
        }
    }
}

Results:

enter image description here

How do I set it such that the list view would fill the navigationBar like the image below:

enter image description here

Note that adding a background color is not preferred as I would like to maintain the navigationBar tint effect when the list view is scrolled.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments