我實作了自己的標簽欄:
struct MainView: View
{
@State var selectedIndex = 0
let icons = ["menucard", "house"]
let iconsNames = ["meniu", "oferte"]
var body: some View{
VStack(spacing: 0){
ZStack{
switch selectedIndex{
case 0:
MeniuListView()
case 1:
ProfileView()
}
Divider()
HStack{
ForEach(0..<2, id: \.self){number in
Spacer()
Button(action: {
self.selectedIndex=number
}, label: {
VStack(spacing: 3){
Image(systemName: icons[number])
.font(.system(size: 25,
weight: .regular,
design: .default))
}
}
}
}
現在的問題是,如果我想進入特定視圖,如何隱藏它?這樣做的最佳方法是什么?例如我想導航到一個login
頁面,但標簽欄沒有隱藏..
這是我ProfileView()
呼叫登錄頁面但標簽欄沒有消失..我怎樣才能隱藏它?組態檔查看代碼:
struct ProfileShopView: View {
@State var goToNextScreen : Int? = nil
var body: some View {
NavigationView{
VStack{
Form{
}
NavigationLink(destination: LoginView().navigationBarHidden(true), tag: 1, selection: $goToNextScreen)
{
EmptyView()
}
Button(action: {
goToNextScreen=1
UserDefaults.standard.set(false, forKey: "isLogin")
} //need to hide the tab bar when navigating to login view
}
}
uj5u.com熱心網友回復:
方法
- 使用全屏封面進行登錄視圖
- 登錄后登錄視圖被關閉
- 使用標簽欄
- 點擊注銷再次顯示登錄視圖
代碼
登錄
struct LoginView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
ZStack {
Color.yellow
Button("Sign in") {
dismiss()
}
.buttonStyle(.bordered)
}
.ignoresSafeArea()
}
}
標簽
enum TabContent: Int {
case menu
case profile
}
內容
struct ContentView: View {
@State private var selection = TabContent.menu
@State private var isLoginShown = true
var body: some View {
NavigationStack {
TabView(selection: $selection) {
Text("Menu list")
.tabItem {
Label("Menu", systemImage: "list.bullet")
}
.tag(TabContent.menu)
VStack {
Text("Profile view")
Button("Logout") {
isLoginShown = true
}
}
.tabItem {
Label("Profile", systemImage: "person.crop.circle")
}
.tag(TabContent.profile)
}
}
.fullScreenCover(isPresented: $isLoginShown) {
LoginView()
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533057.html
標籤:迅速迅捷swiftui-导航链接swiftui-导航视图swiftui-tabview
上一篇:運行reverseGeocodeLocation函式在SwiftPlay中創建例外或不回傳任何內容
下一篇:在多個非托管實體之間共享保留狀態