[英]Show/Hide NavigationBar and Tab bar on tap Gesture in SwiftUI?
當我點擊視圖時,我試圖顯示/隱藏我的NavigationBar和我的標簽欄。
我有一個我想點擊的背景視頻,然后導航欄和標簽欄應該消失,如果我再次點擊它,導航欄和標簽應該會重新出現。
我目前什至無法隱藏導航欄。
我使用這段代碼:
struct FirstView: View {
var body: some View {
NavigationView {
ZStack {
PlayerView()
.edgesIgnoringSafeArea(.all)
.onTapGesture(count: 1) {
print("tapped!")
self.navigationBarHidden(true)
self.navigationBarTitle("", displayMode: .inline)
self.edgesIgnoringSafeArea([.top, .bottom])
}
}
}
}
}
它基本上是我正在嘗試使用的代碼:
.onTapGesture(count: 1) {
print("tapped!")
self.navigationBarHidden(true)
self.navigationBarTitle("", displayMode: .inline)
self.edgesIgnoringSafeArea([.top, .bottom])
}
上面的代碼將被打印出來! 但它並沒有隱藏任何東西。
我在這里想念什么?
你需要有 state 才能隱藏導航欄,像這樣
struct FirstView: View {
@State private var hideNavigationBar = false
var body: some View {
NavigationView {
ZStack {
PlayerView()
.edgesIgnoringSafeArea(.all)
.onTapGesture(count: 1) {
print("tapped!")
self.hideNavigationBar.toggle()
}
}
.navigationBarHidden(hideNavigationBar)
.navigationBarTitle("", displayMode: .inline)
.edgesIgnoringSafeArea([.top, .bottom])
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.