ZPopupPresenter

轻松地在 SwiftUI 上从任何子视图以全屏方式展示不同的弹窗。

特性

安装

Swift Package Manager

https://github.com/Jnis/ZPopupPresenter.git

使用

  1. 创建一个共享的 PresenterModel (例如,通过使用 environment property)
  2. 将 ZPopupPresenterView 添加为您根视图的 overlay
import ZPopupPresenter

struct ContentView: View {
    let zPopupPresenterModel = ZPopupPresenterModel() // 1.1. shared model
    
    var body: some View {
        MyView() 
            .environmentObject(zPopupPresenterModel) // 1.2. inject model for subviews
            .overlay {
                ZPopupPresenterView(model: zPopupPresenterModel) // 2. popups place
            }
    }
}
  1. 调用共享模型的 showPopup 方法,并用 AnyView( ... ) 包装您的视图
  2. 调用 close 闭包来移除弹窗
struct MyView: View {
    @EnvironmentObject var zPopupPresenterModel: ZPopupPresenterModel // shared model
    
    var body: some View {
        VStack {
            Button("Show Popup", action: {
            
                zPopupPresenterModel.showPopup({ close in // 3.
                    AnyView(
                        DemoPopup1View(closeBlock: close) // 4.
                    )
                })
                
            })
        }
    }
}

您可以在 /Examples 文件夹中找到更多示例。

许可证

MIT