中SWIFTUI中想要弹窗TOAST并没有现成的接口
从网上这里 GitHub - elai950/AlertToast: Create Apple-like alerts & toasts using SwiftUI
找到一个,功能挺好,就是用起来很多代码,于是我修改了一下,使用简单多了
struct ContentView: View{
@State private var isShowToast = false
var body: some View{
VStack{
Button("Show Toast"){
AlertToast.showToast(isShow: &isToast, "title" , "subtitle message")
}
} // ****************** <------ Note: this is the outerest View
.toast(isPresenting: $isShowToast){
// `.alert` is the default displayMode
// AlertToast(type: .regular, title: "Message Sent!")
//Choose .hud to toast alert from the top of the screen
//AlertToast(displayMode: .hud, type: .regular, title: "Message Sent!")
//Choose .banner to slide/pop alert from the bottom of the screen
//AlertToast(displayMode: .banner(.slide), type: .regular, title: "Message Sent!")
//
AlertToast(displayMode: .alert, type: .regular ) // this is the commom one , ** use this one **
}
}
}
主要修改了 AlertToast.swift 这个文件这个的 AlertToast 这个 View 的代码:
public struct AlertToast: View{
// @State static var tootShow : Bool = false
static var tootTitle : String = ""
static var tootMsg : String = ""
static func showToast( isShow : inout Bool , _ title : String , _ msg : String = "" ) {
AlertToast.tootTitle = title
AlertToast.tootMsg = msg
isShow = true
// tootShow = true
}
public enum BannerAnimation{
case slide, pop
}
/// Determine how the alert will be display
public enum DisplayMode: Equatable{
///Present at the center of the screen
case alert
///Drop from the top of the screen
case hud
///Banner from the bottom of the view
case banner(_ transition: BannerAnimation)
}
/// Determine what the alert will display
public enum AlertType: Equatable{
///Animated checkmark
case complete(_ color: Color)