Rak*_*esh 6 ios swiftui xcode11
我一直试图viewController在 swiftUI 中为整体设置背景颜色,但我无法做到。视图不带属性.backgroundColor。
我已经使用尝试.backgroundColor的属性viewController中sceneDelegate也是一样,它没有采取属性,但它需要的foregroundColor属性。
Rya*_*yan 14
到目前为止,我发现最有效的方法是在 ContentView 主体的顶部创建一个 ZStack,并将第一层设置为 Color,忽略安全区域。Apple 将 Color 定义为“后期绑定令牌”,不管它是什么,但它的行为与任何其他 View 相似。
struct ContentView: View {
var body: some View {
ZStack {
Color.red
.edgesIgnoringSafeArea(.all)
/// Your Inner View content goes here.
VStack {
Text("Hello")
} // VStack
} // ZStack
} // body View
} // ContentView
Run Code Online (Sandbox Code Playgroud)
注意:小心内部视图里面的东西,因为它很容易扩展以填充整个窗口,从而用白色覆盖你的背景颜色等。例如,流行的NavigationView倾向于扩展到整个窗口,对我来说它倾向于忽略其记录的 .background() 设置。您也可以在这里执行相同的 ZStack 策略。
NavigationView {
ZStack {
Color.red.edgesIgnoringSafeArea(.all)
VStack {
Text("Hello")
} // VStack
} // ZStack
} // NavigationView
Run Code Online (Sandbox Code Playgroud)
小智 3
如果您在顶部视图上使用背景修饰符(.background(Color.red)),您将用一个新视图包装该视图,其填充或大小周围的背景色为红色。因此,您应该使用 Rectangle() 视图和 Z 堆栈来为整个视图设置完整背景,如果需要,请使用 .edgesIgnoringSafeArea
NavigationView {
ZStack {
Rectangle().foregroundColor(.blue).edgesIgnoringSafeArea(.all)
ScrollView {
List()
ForEach(modelExampleList) { modelExample in
Row(model: modelExample)
}
}.frame(height: 200)
}.padding(.leading, 10)
}.navigationBarTitle(Text("ModelExample List"), displayMode: .large)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7433 次 |
| 最近记录: |