如何在SwiftUI中动态推送视图或显示视图?
我正在学习SwiftUI,目前我的重点是实现一个我能够使用UIKit实现的方法。我需要创建一个方法,该方法根据布尔值确定是推送视图还是以模态方式呈现视图 在UIKit中,我的代码是:如何在SwiftUI中动态推送视图或显示视图?,swiftui,presentmodalviewcontroller,swiftui-navigationlink,swiftui-navigationview,Swiftui,Presentmodalviewcontroller,Swiftui Navigationlink,Swiftui Navigationview,我正在学习SwiftUI,目前我的重点是实现一个我能够使用UIKit实现的方法。我需要创建一个方法,该方法根据布尔值确定是推送视图还是以模态方式呈现视图 在UIKit中,我的代码是: var presentVC = true // boolean that determines whether VC will be presented or pushed let vc = ViewController() //Your VC that will be pushed or presented
var presentVC = true // boolean that determines whether VC will be presented or pushed
let vc = ViewController() //Your VC that will be pushed or presented
if (presentVC == true) {
self.presentViewController(vc, animated: true, completion: nil)
} else {
self.navigationController.pushViewController(vc, animated: true)
}
但在SwiftUI中,我不确定如何使用以下工具正确实现这一点:
- 导航链接-用于推送视图
- .sheet(显示:,内容:)-用于以模式显示视图
我正在使用SwiftUI 1.0,因为我需要支持iOS 13。一个可能的解决方案是使用可用的表示类型创建自定义枚举:
enum PresentationType {
case push, sheet // ...
}
并创建用于激活不同视图的自定义绑定:
func showChildView(presentationType: PresentationType) -> Binding<Bool> {
.init(
get: { self.showChildView && self.presentationType == presentationType },
set: { self.showChildView = $0 }
)
}
func showChildView(presentationType:presentationType)->绑定{
.init(
获取:{self.showChildView&&self.presentationType==presentationType},
集合:{self.showChildView=$0}
)
}
完整代码:
struct ContentView: View {
@State var presentationType = PresentationType.push
@State var showChildView = false
func showChildView(as presentationType: PresentationType) -> Binding<Bool> {
.init(
get: { self.showChildView && self.presentationType == presentationType },
set: { self.showChildView = $0 }
)
}
var body: some View {
NavigationView {
VStack {
Button(action: {
self.presentationType = .push
self.showChildView = true
}) {
Text("Present new view as Push")
}
Button(action: {
self.presentationType = .sheet
self.showChildView = true
}) {
Text("Present new view as Sheet")
}
}
.navigationBarTitle("Main view", displayMode: .inline)
.background(
NavigationLink(
destination: ChildView(),
isActive: self.showChildView(presentationType: .push),
label: {}
)
)
}
.sheet(isPresented: self.showChildView(presentationType: .sheet)) {
ChildView()
}
}
}
struct ChildView: View {
var body: some View {
ZStack {
Color.red
Text("Child view")
}
}
}
struct ContentView:View{
@状态变量presentationType=presentationType.push
@状态变量showChildView=false
func showChildView(作为presentationType:presentationType)->绑定{
.init(
获取:{self.showChildView&&self.presentationType==presentationType},
集合:{self.showChildView=$0}
)
}
var body:一些观点{
导航视图{
VStack{
按钮(操作:{
self.presentationType=.push
self.showChildView=true
}) {
文本(“将新视图显示为推送”)
}
按钮(操作:{
self.presentationType=.sheet
self.showChildView=true
}) {
文本(“将新视图显示为图纸”)
}
}
.navigationBarTitle(“主视图”,显示模式:。内联)
.背景(
导航链接(
目标:ChildView(),
isActive:self.showChildView(呈现类型:.push),
标签:{}
)
)
}
.sheet(显示:self.showChildView(显示类型:.sheet)){
ChildView()
}
}
}
结构子视图:视图{
var body:一些观点{
ZStack{
颜色:红色
文本(“子视图”)
}
}
}
@swiftPunk,我知道怎么用谷歌搜索。我还知道如何实现NavigationLink和单独显示模态的工作表。我问这个问题是因为我找不到任何关于我面临的这种情况的文章。如果你看到了一篇文章,请在这里标记链接,我们将不胜感激。谢谢如果你知道所有这些,你也应该知道答案,我们会为得到答案而带来错误或错误的编码方式,而不是免费询问code@swiftPunk,也许你不明白这个问题。这正是我试图在SwiftUI中实现的。非常感谢你,波韦洛2222!