有没有办法可以指定用于在 SwiftUI 中显示工作表的视图的背景颜色?我知道我可以设置.background我的视图,但在 iPhone X 上不会影响安全区域。我也可以.edgesIgnoringSafeArea用来填充该区域,但我不想弄乱它。
小智 12
另一种解决方案是使用ZStack:
.sheet(isPresented: $showingCustomView) {
ZStack {
Color.black.edgesIgnoringSafeArea(.all)
CustomView()
}
}
Run Code Online (Sandbox Code Playgroud)
最后你需要处理,edgesIgnoringSafeArea因为你需要超越安全区域(你必须在 UIKit 中做同样的事情)。重要的是,您只忽略背景的安全区域。所以在 SwiftUI 方面,你只需要应用edgesIgnoringSafeArea到你传递给的视图background:
struct ContentView: View {
var body: some View {
VStack {
Text("hey there")
Text("Another line")
}.background(Color.blue.edgesIgnoringSafeArea(.all))
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这要求您使用的视图background已经请求了所有可用空间(在我上面的示例代码中不是这种情况),因为背景仅绘制视图的背景,而不会自行填充屏幕。如果您不是这种情况,则需要使用例如 aZStack如您的答案所示。
又搞乱了一些并找到了这个解决方案
struct BackgroundFillView<Content: View>: View {
let backgroundColor: Color
let content: () -> Content
var body: some View {
ZStack {
self.backgroundColor.edgesIgnoringSafeArea(.all)
self.content()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3058 次 |
| 最近记录: |