如何在 SwiftUI 中以清晰/透明背景设置导航栏? [英] How to set a navigation bar in clear / transparent background in SwiftUI?
本文介绍了如何在 SwiftUI 中以清晰/透明背景设置导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想弄清楚如何为自定义导航栏编写代码以显示清晰/透明的栏而不是白色"酒吧.请参阅此屏幕截图:
I am trying to figure out how to write a code for a custom navigation bar to display clear / transparent bar not "white" bar. See this screenshot:
这是我的代码:
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()
}
}
有人知道这是怎么回事吗?
Does anybody know what is wrong with it?
推荐答案
我尝试在我的 Xcode 上运行您的代码.我收到了和你一样的结果.我找到了一个很好的解决方案来解决这个问题.您只需要在 init()
中添加几行代码即可.解决办法如下:
I tried to run your code on my Xcode. I received the same results like yours. I found a good solution to fix this issue. You just need to add a few lines of code into your init()
. Here is the solution:
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()
}
}
希望能帮到你.
这篇关于如何在 SwiftUI 中以清晰/透明背景设置导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文