ios - SwiftUI - 视图获取隐藏在导航栏下
问题描述
在我的应用程序中,我想隐藏 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()
}
}
}
如何保持导航栏完全不透明且没有底线并使推送视图正常工作?
解决方案
所以我最终通过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())
}
}
}
推荐阅读
- laravel - 是否可以在 Laravel 5.8 中“锁定”会话?
- python - 网络抓取表可以从错误的数据中正确读取
- python - 结合 .start() 的 Python3 线程不会创建连接属性
- python - 返回没有被放置在右块
- python - 添加节点功能不起作用单链表python
- python-3.x - Python:除非指定文件,否则从 STDIN 读取,它是如何完成的?
- javascript - 从数组中删除成员时,我们如何访问该成员?
- android - 如何获取登录 api 的响应以使用 volley 对此 json 数据执行登录,内容类型为:application/x-www-form-urlencoded
- python - 将制表符分隔的 txt 文件转换为逗号分隔的 csv 文件
- c - 空指针为什么我得到这个输出?