SwiftUI:关闭第一个工作表时如何显示第二个工作表
Posted
技术标签:
【中文标题】SwiftUI:关闭第一个工作表时如何显示第二个工作表【英文标题】:SwiftUI: How to display the second Sheet when first sheet is closed 【发布时间】:2021-04-20 06:48:08 【问题描述】:当用户登录并按下 MyGarage 按钮时,我想使用 .fullScreenCover
创建一个侧边菜单。 .fullScreenCover
将关闭,主视图将导航到 MyGarage 视图。但如果用户未登录,.fullScreenCover
将关闭,并出现带有.fullScreenCover
的 loginView。我的问题是,如果我在主视图中放置 2 个相同的 .fullScreenCover
,.fullScreenCover
将不起作用。有没有办法解决这个问题?抱歉,我有点难以解释。
这是代码
侧边菜单视图
struct SideMenuView: View
@Environment(\.presentationMode) var presentationMode
@Binding var showMyGarage: Bool
@Binding var showSignIn: Bool
var user = 0 //If user is 1, it is logged in
var body: some View
NavigationView
VStack
Button(action:
presentationMode.wrappedValue.dismiss()
if user == 1
self.showMyGarage = true
else
self.showSignIn = true
, label:
Text("My Garage")
)
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(leading:
HStack(spacing: 20)
Button(action:
presentationMode.wrappedValue.dismiss()
, label:
Text("X")
)
Text("Main Menu")
)
.navigationViewStyle(StackNavigationViewStyle())
主视图
struct HomeView: View
@State var showSideMenu = false
@State private var showMyGarage = false
@State var showSignIn = false
var body: some View
VStack
Text("Home")
NavigationLink(destination: MyGarageView(showMyGarage: $showMyGarage), isActive: $showMyGarage)
EmptyView()
.navigationBarItems(leading:
Button(action:
self.showSideMenu.toggle()
, label:
Text("Menu")
)
)
.fullScreenCover(isPresented: $showSideMenu, content:
SideMenuView(showMyGarage: $showMyGarage, showSignIn: $showSignIn)
)
.fullScreenCover(isPresented: $showSignIn, content:
SignInView()
)
struct MyGarageView: View
@Binding var showMyGarage: Bool
var body: some View
Text("MyGarage")
struct SignInView: View
var body: some View
Text("Sign In")
【问题讨论】:
【参考方案1】:尝试将它们附加到不同的视图,例如
var body: some View
VStack
Text("Home")
.fullScreenCover(isPresented: $showSideMenu, content:
SideMenuView(showMyGarage: $showMyGarage, showSignIn: $showSignIn)
)
NavigationLink(destination: MyGarageView(showMyGarage: $showMyGarage), isActive: $showMyGarage)
EmptyView()
.fullScreenCover(isPresented: $showSignIn, content:
SignInView()
)
.navigationBarItems(leading:
Button(action:
self.showSideMenu.toggle()
, label:
Text("Menu")
)
)
【讨论】:
以上是关于SwiftUI:关闭第一个工作表时如何显示第二个工作表的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI - 如何将视图关闭到根目录,然后立即推送第二个视图?