选项卡
我实现了自己的标签栏:
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()
调用登录页面,但标签栏没有消失。我怎么能把它藏起来?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
}
}
方法
- 使用全屏覆盖登录视图
- 登录后注销登录视图
- 使用标签栏
- 点击登出再次显示登录视图
登录
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()
}
}
}
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()
}
}
}