我试图弄清楚如何为自定义导航栏编写代码以显示清晰/透明的栏而不是“白色”栏。看这个截图:
这是我的代码:
import SwiftUI
struct ContentView: View {
init() {
UINavigationBar.appearance().tintColor = .clear
UINavigationBar.appearance().backgroundColor = .clear
}
var body: some View {
NavigationView {
ZStack {
Color(.lightGray).edgesIgnoringSafeArea(.all)
VStack() {
Spacer()
Text("Hello").foregroundColor(.white)
Spacer()
}
}
.navigationBarTitle(Text("First View"), displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
有人知道这是怎么回事吗?
我尝试在我的 Xcode 上运行你的代码。我收到了和你一样的结果。我找到了一个很好的解决方案来解决这个问题。您只需要添加几行代码到您的init()
。这是解决方案:
import SwiftUI
struct ContentView: View {
init() {
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: UIBarMetrics.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = true
UINavigationBar.appearance().tintColor = .clear
UINavigationBar.appearance().backgroundColor = .clear
}
var body: some View {
NavigationView {
ZStack {
Color(.lightGray).edgesIgnoringSafeArea(.all)
VStack() {
Spacer()
Text("Hello").foregroundColor(.white)
Spacer()
}
}
.navigationBarTitle(Text("First View"), displayMode: .inline)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我希望这对你有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)