SwiftUI 中的 toast 弹窗

这篇博客介绍了如何在SwiftUI中实现toast弹窗,原生接口并未提供该功能。作者参考了一个GitHub项目并进行了简化,使得使用更加便捷。主要内容涉及对AlertToast.swift文件中AlertToast视图的修改,实现了类似Apple风格的弹窗效果。
摘要由CSDN通过智能技术生成

中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)
        
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值