在SwiftUI中将TabView与PageTabViewStyle,NavigationView和ScrollView正确组合

时间:2020-09-29 11:03:33

标签: swiftui

我正在使用一个将TabView与PageTabViewStyle,NavigationView和ScrollView结合在一起的UI。向下滚动时,我希望NavigationView的标题可以调整并缩小。但是,当ScrollView置于TabView内时,则不会。如何实现这种行为?

这是我原来的question的扩展,它涉及如何将TabView与PageTabViewStyle和NavigationView一起使用。

struct ContentView: View {
    @State var selection = 1
    
    var body: some View {
        NavigationView {
            TabView {
                LibraryView()
                    .tag(1)
                LibraryView()
                    .tag(2)
            }
            .navigationTitle(selection == 1 ? "A" : "B")
        }
        .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
        //.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .always))
    }
}
struct LibraryView: View {
    
    @State var gridLayout: [GridItem] = [
        GridItem(),
        GridItem()
    ]
    
    var body: some View {
        ScrollView {
            LazyVGrid(columns: gridLayout, alignment: .center, spacing: 10) {
                ForEach(collections.indices) { index in
                    CollectionItem(collection: collections[index])
                }
            }
            .padding(.all, 10)
        }
    }
}

0 个答案:

没有答案