SwiftUI - 视图获取隐藏在导航栏下
Posted
技术标签:
【中文标题】SwiftUI - 视图获取隐藏在导航栏下【英文标题】:SwiftUI - view get's hidden under navigation bar 【发布时间】: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()
如何保持导航栏完全不透明,没有底线,并使推送视图正常工作?
【问题讨论】:
这是否回答了您的问题***.com/a/62114766/12299030? 是的,通过背景修饰符自定义导航栏对我有用 【参考方案1】:感谢Asperi 我遵循了已接受的答案here
所以我最终通过background
修饰符修改了导航栏。我现在的代码:
struct NavigationConfiguration: UIViewControllerRepresentable
init()
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = .clear
navBarAppearance.backgroundColor = .white
UINavigationBar.appearance().standardAppearance = navBarAppearance
func makeUIViewController(
context: UIViewControllerRepresentableContext<NavigationConfiguration>
) -> UIViewController
UIViewController()
func updateUIViewController(_ uiViewController: UIViewController,
context: UIViewControllerRepresentableContext<NavigationConfiguration>)
在我看来:
struct ContentView: View
var body: some View
NavigationView
VStack
NavigationLink(
destination: AnotherView(),
label:
Text("Screen 1")
)
Spacer()
.navigationBarTitleDisplayMode(.inline)
.background(NavigationConfiguration())
【讨论】:
以上是关于SwiftUI - 视图获取隐藏在导航栏下的主要内容,如果未能解决你的问题,请参考以下文章