[英]Navigation Bar hide is not working in SwiftUI
我有三观。 我想在第三个视图中隐藏导航栏。 即使我给.navigationBarHidden(true)
导航栏正在显示!
我找不到我做错的地方。 我在下面附上了我的代码和生成的屏幕截图。
Xcode 版本 - 11.1
struct ContentViewOne: View {
var body: some View {
NavigationView {
ZStack {
Color.yellow.edgesIgnoringSafeArea(.all)
VStack(spacing: 20) {
Text("View One")
NavigationLink(destination: ContentViewTwo()) {
Text("Navigate to View Two")
.foregroundColor(.white)
.padding()
.background(Color.red)
}
}
}
.navigationBarTitle("View One")
}
}
}
struct ContentViewTwo: View {
var body: some View {
ZStack {
Color.green.edgesIgnoringSafeArea(.all)
VStack(spacing: 20) {
Text("View Two")
NavigationLink(destination: ContentViewThree()) {
Text("Navigate to View Three")
.foregroundColor(.white)
.padding()
.background(Color.red)
}
}
}
.navigationBarTitle("View Two")
}
}
struct ContentViewThree: View {
var body: some View {
ZStack {
Color.gray.edgesIgnoringSafeArea(.all)
Text("View Three")
}
.navigationBarTitle("View Three")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
注意:(出于某种原因,它在某些情况下有效) SwiftUI
要求您需要.navigationBarTitle
才能使.navigationBarHidden
正常工作。
NavigationView {
ScrollView() {
......
}.
.navigationBarTitle("") //this must be empty
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
我尝试了多种解决方案,包括 UINavigationControllerDelegate 并且似乎没有任何东西可以使导航栏永久隐藏。 直到我尝试了 KVO :)
所以如果你想要一个永久的解决方案,使用这个:
struct NoBarNavigationView<Content: View>: View {
private let content: Content
init(@ViewBuilder content: () -> Content) {
self.content = content()
}
var body: some View {
NavigationView {
content
.introspectNavigationController { (UINavigationController) in
NavigationControllerDelegate.shared.becomeDelegate(of: UINavigationController)
}
}
}
}
class NavigationControllerDelegate: NSObject {
static let shared = NavigationControllerDelegate()
func becomeDelegate(of navigationController: UINavigationController) {
navigationController.isNavigationBarHidden = true
navigationController.navigationBar.isHidden = true
navigationController.navigationBar.addObserver(self, forKeyPath: "alpha", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// This is necessary to ensure the UINavigationBar remains hidden
if let navigationBar = object as? UINavigationBar {
navigationBar.isHidden = true
}
}
}
快乐编码!
编辑:正如评论中指出的,我正在使用:
import Introspect
在您的 3rd View
中专门隐藏NavigationBar
。 您必须删除.navigationBarTitle("View Three")
并且该栏被隐藏:
struct ContentViewThree: View {
var body: some View {
ZStack {
Color.gray.edgesIgnoringSafeArea(.all)
Text("View Three")
}
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
}
}
发布此内容以提高处理 SwiftUI NavigationBar 不隐藏或隐藏时仍占用空间的可见性:
.navigationBarHidden(true)
.navigationBarTitle("", displayMode: .inline)
.edgesIgnoringSafeArea([.top, .bottom])
这设置了一个标题(旧的 swiftUI 版本有时需要 hack),隐藏栏,但也告诉渲染引擎忽略为导航栏保留空间的任何安全区域(假定)。
您需要在 navigationView 上添加 .navigationBarHidden(true),或者如果您有 NavigationLink,则需要在 Link 上添加
NavigationLink("",
destination: Text("TEST"),
tag: linkValue,
selection: $linksNavigator.selection)
**.navigationBarHidden(true)**
因为您应该将隐藏属性添加到 DetailView 而不是 NavigationLink,
NavigationLink(destination: DetailView(mData: sportData)
.navigationBarTitle("")
.navigationBarHidden(true)
.navigationBarBackButtonHidden(true)
) {
Image("arrowRight3").resizable()
//.padding(5)
//.background(.red)
在 iOS 15 中,您可以使用 @State 属性来切换状态。
@State private var hideNavigationbar: Bool = false
您将.onAppear {}中的值切换为 true。 当您关闭视图时,您调用.onDisappear {}并将属性设置为 false。
现在你可以像这样使用它:
.navigationbarHidden(hideNavigationbar)
使用 iOS 15.5 和 15.6 .navigationBarHidden(true)
根本不起作用。 添加.navigationBarTitle("", displayMode: .inline)
也无济于事。 这显然是 iOS 15 中的一个错误。
它已在 iOS 16 中修复!
Tested with Xcode 14 Beta 5 Simulated iOS 15.5, iPhone w/ iOS 15.6 and Simulated iOS 16, the bug got fixed in iOS 16.
最新隐藏方法,适用于iOS15
.navigationBarHidden(true)
.ignoresSafeArea()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.