【发布时间】:2021-01-11 11:33:16
【问题描述】:
在我的应用程序中,我想隐藏 UINavigationBar 的底线。为此,我使用 UINavigationBar 的外观()访问器。像这样:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().barTintColor = .white
}
}
但是当我这样做时,从主视图推送的视图内容会被导航栏重叠。出于某种原因,当我将isTranslucent 设置为true 时,推送的视图正常工作,但在这种情况下,导航栏完全是半透明的,并且滚动上的任何内容都在其后面可见,我不希望这种行为。
这是我的观点的代码:
struct ContentView: View {
var body: some View {
NavigationView {
VStack {
NavigationLink(
destination: AnotherView(),
label: {
Text("Screen 1")
})
Spacer()
}
.navigationBarTitleDisplayMode(.inline)
}
}
}
struct AnotherView: View {
var body: some View {
VStack {
Text("Screen 2")
Spacer()
}
}
}
如何保持导航栏完全不透明,没有底线,并使推送视图正常工作?
【问题讨论】:
-
这是否回答了您的问题stackoverflow.com/a/62114766/12299030?
-
是的,通过背景修饰符自定义导航栏对我有用