在 SwiftUI 中呈现新视图 [英] Present a new view in SwiftUI

查看:23
本文介绍了在 SwiftUI 中呈现新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想点击一个按钮,然后在 UIKit 中呈现一个像 present modally 这样的新视图

I want to click a button and then present a new view like present modally in UIKit

我已经看过如何使用工作表呈现新视图",但我不想将其作为模态工作表附加到主视图.

I have already seen "How to present a new view using sheets", but I don't want to attach it to the main view as a modal sheet.

而且我不想使用NavigationLink,因为我不希望新视图和旧视图有导航关系.

And I don't want to use NavigationLink, because I don't want a new view and old view have a navigation relationship.

感谢您的帮助...

推荐答案

显示模态(iOS 13 风格)

你只需要一个简单的sheet,并能自行解除:

struct ModalView: View {
    @Binding var presentedAsModal: Bool
    var body: some View {
        Button("dismiss") { self.presentedAsModal = false }
    }
}

并以如下方式呈现:

struct ContentView: View {
    @State var presentingModal = false
    
    var body: some View {
        Button("Present") { self.presentingModal = true }
        .sheet(isPresented: $presentingModal) { ModalView(presentedAsModal: self.$presentingModal) }
    }
}

注意我将 presentingModal 传递给了模态,这样你就可以从模态本身中消除它,但你可以摆脱它.

Note that I passed the presentingModal to the modal so you can dismiss it from the modal itself, but you can get rid of it.

您需要访问ViewController.所以你需要一些辅助容器和环境的东西:

You need to access to the ViewController. So you need some helper containers and environment stuff:

struct ViewControllerHolder {
    weak var value: UIViewController?
}

struct ViewControllerKey: EnvironmentKey {
    static var defaultValue: ViewControllerHolder {
        return ViewControllerHolder(value: UIApplication.shared.windows.first?.rootViewController)

    }
}

extension EnvironmentValues {
    var viewController: UIViewController? {
        get { return self[ViewControllerKey.self].value }
        set { self[ViewControllerKey.self].value = newValue }
    }
}

那么你应该使用实现这个扩展:

Then you should use implement this extension:

extension UIViewController {
    func present<Content: View>(style: UIModalPresentationStyle = .automatic, @ViewBuilder builder: () -> Content) {
        let toPresent = UIHostingController(rootView: AnyView(EmptyView()))
        toPresent.modalPresentationStyle = style
        toPresent.rootView = AnyView(
            builder()
                .environment(.viewController, toPresent)
        )
        NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "dismissModal"), object: nil, queue: nil) { [weak toPresent] _ in
            toPresent?.dismiss(animated: true, completion: nil)
        }
        self.present(toPresent, animated: true, completion: nil)
    }
}

终于

你可以像这样全屏:

struct ContentView: View {
    @Environment(.viewController) private var viewControllerHolder: UIViewController?
    
    var body: some View {
        Button("Login") {
            self.viewControllerHolder?.present(style: .fullScreen) {
                Text("Main") // Or any other view you like
// uncomment and add the below button for dismissing the modal
            // Button("Cancel") {
            //       NotificationCenter.default.post(name: Notification.Name(rawValue: "dismissModal"), object: nil)
            //        }
            }
        }
    }
}

这篇关于在 SwiftUI 中呈现新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆