轻松弹出任何使用SwiftUI编写的视图!
使用SwiftUI实现的弹出控件,简单易用!《中文文档》
SwiftUI 制作的 PopupView 简单易用!『日本語のREADME』
dependencies: [
.package(url: "https://github.com/pikacode/PopupUI.git")
]
pod 'PopupUI'
import PopupUI
在视图后添加 .popupUI()
以在其实例内弹出
var body: some View {
VStack {
...
}
.popupUI() // <-- Add to the view
}
或者 添加到根视图,一次即可在整个应用程序中弹出
@main
struct PopupUI_demoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.popupUI() // <-- Add to the root view
}
}
}
PopupUI
.show(Text("Hello, PopupUI!"))
PopupUI
.hide() // <-- Hide the last popup
自定义各种参数
PopupUI
.show(YourCustomView()) // <-- The view to be shown
.from(.bottom) // <-- The direction from which the view is shown
.stay(2) // <-- The duration of the view staying
.to(.center, .easeOut(duration: 0.3)) // <-- The direction to which the view is hidden and the animation
.background(Color.black.opacity(0.3)) // <-- The background view
.padding(24) // <-- The padding of the view
.isSafeArea(true) // <-- Whether to avoid the safe area
.id("Unique Popup ID") // <-- The unique identifier, when not passed, the same id is used by default, so only one popup can be displayed at a time, you can display multiple popups at the same time by setting different ids
.isAvoidKeyboard(true) // <-- Whether to avoid the keyboard
.isOpaque(true) // <-- Whether to prevent the user from interacting with the background view
.dismissWhenTapBackground(true) // <-- Whether to hide when the background view is tapped
.dismissCallback { id in // <-- The callback when the view is hidden
print("Popup dismissed: \(id)")
}
或者显示一个视图并通过闭包自定义参数
PopupUI
.show {
VStack {
...
}
} config: { config in
config.from = ...
}
PopupUI
.hide("Unique Popup ID") // <-- Hide the specified popup
通过 PopupConfiguration.default
自定义默认参数,以简化弹出代码
let configuration = PopupConfiguration()
configuration.stay = 2
configuration.to = .center
...
PopupConfiguration.default = configuration
或者
PopupConfiguration
.default
.stay(2)
.to(.center)
...