[英]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.