本文介绍了在SwiftUI中更改具有NavigationView和ScrollView的TabView内View的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用4个带有收集视图的选项卡构建一个TabView
.下面是我的一个名为图库"标签的代码.
I want to build a TabView
with 4 tabs having collection views in it. Below is my code of one tab named 'Gallery'.
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颜色怎么办?
When I give background color for ScrollView
, scrolling is not working for NavigationView
largeTitle. How can I achieve this, I want to give red color for full view's background? What if I need to achieve this same backgorund color for all tabs?
推荐答案
这里是可行的方法(在这种情况下滚动视图不会损坏)
Here is possible approach (scroll view is not broken in such case)
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")
}
}
这篇关于在SwiftUI中更改具有NavigationView和ScrollView的TabView内View的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!