How to Toast message in Swift?

后端 未结 22 1133
暖寄归人
暖寄归人 2020-12-22 19:18

Is there any way to Toast message in swift ?

I have tried in objective c but could not find solution in swift.

[self.view makeToast:@\"Account create         


        
22条回答
  •  独厮守ぢ
    2020-12-22 19:50

    I have been using this extension when ever i need toast-message like android.. Just copy the extension to you project and then in your UIViewController class, call the function like

    self.toastMessage("Downloading...") 
    // Extention is below
    
    extension UIViewController {
      func toastMessage(_ message: String){
        guard let window = UIApplication.shared.keyWindow else {return}
        let messageLbl = UILabel()
        messageLbl.text = message
        messageLbl.textAlignment = .center
        messageLbl.font = UIFont.systemFont(ofSize: 12)
        messageLbl.textColor = .white
        messageLbl.backgroundColor = UIColor(white: 0, alpha: 0.5)
    
        let textSize:CGSize = messageLbl.intrinsicContentSize
        let labelWidth = min(textSize.width, window.frame.width - 40)
    
        messageLbl.frame = CGRect(x: 20, y: window.frame.height - 90, width: labelWidth + 30, height: textSize.height + 20)
        messageLbl.center.x = window.center.x
        messageLbl.layer.cornerRadius = messageLbl.frame.height/2
        messageLbl.layer.masksToBounds = true
        window.addSubview(messageLbl)
    
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
    
        UIView.animate(withDuration: 1, animations: {
            messageLbl.alpha = 0
        }) { (_) in
            messageLbl.removeFromSuperview()
        }
        }
    }}
    

提交回复
热议问题