【问题标题】:How to Push View or Present View Modally in SwiftUI dynamically?如何在 SwiftUI 中动态推送视图或呈现视图?
【发布时间】:2021-02-09 03:10:17
【问题描述】:

我正在学习 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

if (presentVC == true) {
     self.presentViewController(vc, animated: true, completion: nil)
} else {
    self.navigationController.pushViewController(vc, animated: true)
}

但在 SwiftUI 中,我不确定如何正确使用:

  • NavigationLink - 用于推送视图
  • .sheet(isPresented:, content:) - 用于以模态方式呈现视图

似乎 NavigationLink 和 .sheet 修饰符与视图实现相结合。 是否有人已经在 SwiftUI 中遇到并解决了这种情况?谢谢

我正在使用 SwiftUI 1.0,因为我需要支持 iOS 13。

【问题讨论】:

  • @swiftPunk,我知道怎么用谷歌搜索。我也知道如何实现 NavigationLink 和工作表来分别呈现 Modal。我问了这个问题,因为我找不到任何关于我面临的这种情况的文章。如果您设法看到一篇文章,请在此处标记链接,我们将不胜感激。谢谢
  • 如果你知道这一切,你也应该知道答案,我们配偶带来我们的错误或错误的编码方式来获得答案,而不是要求免费代码
  • @swiftPunk,也许你不明白这个问题。

标签: swiftui presentmodalviewcontroller swiftui-navigationlink swiftui-navigationview


【解决方案1】:

一种可能的解决方案是创建一个具有可用表示类型的自定义枚举:

enum PresentationType {
    case push, sheet // ...
}

并创建自定义绑定以激活不同的视图:

func showChildView(presentationType: PresentationType) -> Binding<Bool> {
    .init(
        get: { self.showChildView && self.presentationType == presentationType },
        set: { 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")
        }
    }
}

【讨论】:

  • 这正是我试图在 SwiftUI 中实现的。非常感谢 paello2222!
猜你喜欢
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多