Ily*_*bet 10 ios swift swiftui
我正在尝试使用以下代码更改 NavigationView(不是导航栏)的背景颜色:
NavigationView {
Text("Text")
}
.background(Color.clear)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。另外,我尝试更改 UIView 外观:
UIView.appearance().backgroundColor = UIColor.black
Run Code Online (Sandbox Code Playgroud)
但它也不起作用。
实际结果如下:
想要的结果是:
有谁知道如何做到这一点?
先看这个结果:
如您所见,您可以像这样设置 View 层次结构中每个元素的颜色:
struct ContentView: View {
init(){
UINavigationBar.appearance().backgroundColor = .green
//For other NavigationBar changes, look here:(/sf/answers/4025668881/)
}
var body: some View {
ZStack {
Color.yellow
NavigationView {
ZStack {
Color.blue
Text("Some text")
}
}.background(Color.red)
}
}
}
Run Code Online (Sandbox Code Playgroud)
第一个是window
:
window.backgroundColor = .magenta
Run Code Online (Sandbox Code Playgroud)
您面临的问题是我们无法删除 SwiftUI 的背景颜色HostingViewController
(尚),因此您无法navigationView
通过视图层次结构看到(您调用的内容)。您应该等待 API 或尝试伪造导航视图(不推荐)。
归档时间: |
|
查看次数: |
3459 次 |
最近记录: |