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

查看:107
本文介绍了在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)
    }
}

最后

您可以将其设置为fullscreen,例如:

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天全站免登陆