在SwiftUI中更改具有NavigationView和ScrollView的TabView内View的背景颜色

时间:2020-05-15 05:57:53

标签: ios swift swiftui

我想构建一个TabView,其中包含4个带有集合视图的选项卡。以下是我的一个名为“图库”标签的代码。

 var body: some View {
        NavigationView {
            ScrollView {
                GridStack(rows: 3, columns: 2) { row, column, totalColumn in
                    CardView(card: self.cards[(row * totalColumn) + column])
                }.padding().background(Color.red)
            }
            .navigationBarTitle("Gallery")
        }
    }

当我为ScrollView提供背景颜色时,滚动不适用于NavigationView largeTitle。我要如何实现此目的?我想给全视图背景添加红色?如果我需要为所有标签实现相同的backgorund颜色怎么办?

enter image description here

1 个答案:

答案 0 :(得分:2)

这是可行的方法(在这种情况下滚动视图不会中断)

NavigationView {
    GeometryReader { gp in
        ScrollView {
            ZStack(alignment: .top) {
                Rectangle().fill(Color.red) // << background

              // ... your content here, internal alignment might be needed

            }.frame(minHeight: gp.size.height)

        }
        .navigationBarTitle("Gallery")
    }
}
相关问题