swiftui - 如何动态地在 SwiftUI 中以模态方式推送 View 或呈现 View ?

标签 swiftui presentmodalviewcontroller swiftui-navigationlink swiftui-navigationview

我正在学习 SwiftUI,目前我的重点是实现一种我能够使用 UIKit 实现的方法。我需要创建一个方法来确定我是应该推送 View 还是根据 bool 值以模态方式呈现 View 。

在 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 - 用于推送 View
  • .sheet(isPresented:, content:) - 以模态方式呈现 View

NavigationLink 和 .sheet 修饰符似乎与 View 实现耦合在一起。 有没有人已经在 SwiftUI 中遇到并解决了这种情况?谢谢

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

最佳答案

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

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

并创建自定义绑定(bind)以激活不同的 View :

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 - 如何动态地在 SwiftUI 中以模态方式推送 View 或呈现 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66112447/

相关文章:

swift - 使用@fetchRequest(实体: ) for SwiftUI macOS app crashes

iphone - 无法在模态视图 Controller 上执行模态

ios - SwiftUI NavigationLink 自动弹出,这是意外的

ios - 如何将数据从父模型类传递到 SwiftUI 中的 subview

ios - 在swiftUi中删除并添加模型中的数据时,不会发生过渡

ios - 更新到 Xcode 13,尝试在 iOS 但不是 macOS 上运行 SwiftUI 应用程序时获得唯一的错误代码

iphone - 无法让presentViewController 工作

objective-c - 当前模态和解除 View Controller 无法正常工作

swift - iOS 14、Swift UI 2 更改 NavigationLink 内所选列表项的背景颜色

core-data - swiftui 如何从详细信息 View 获取核心数据值到编辑 View