How to Toast message in Swift?

后端 未结 22 1179
暖寄归人
暖寄归人 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:38

    Just add below method. This will show message in different colors with animation (message appearing from left to right & disappear).

    Swift 3.0 -

    class Toast
    {
        class private func showAlert(backgroundColor:UIColor, textColor:UIColor, message:String)
        {
    
            let appDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
            let label = UILabel(frame: CGRect.zero)
            label.textAlignment = NSTextAlignment.center
            label.text = message
            label.font = UIFont(name: "", size: 15)
            label.adjustsFontSizeToFitWidth = true
    
            label.backgroundColor =  backgroundColor //UIColor.whiteColor()
            label.textColor = textColor //TEXT COLOR
    
            label.sizeToFit()
            label.numberOfLines = 4
            label.layer.shadowColor = UIColor.gray.cgColor
            label.layer.shadowOffset = CGSize(width: 4, height: 3)
            label.layer.shadowOpacity = 0.3
            label.frame = CGRect(x: appDelegate.window!.frame.size.width, y: 64, width: appDelegate.window!.frame.size.width, height: 44)
    
            label.alpha = 1
    
            appDelegate.window!.addSubview(label)
    
            var basketTopFrame: CGRect = label.frame;
            basketTopFrame.origin.x = 0;
    
            UIView.animate(withDuration
                :2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseOut, animations: { () -> Void in
                    label.frame = basketTopFrame
            },  completion: {
                (value: Bool) in
                UIView.animate(withDuration:2.0, delay: 2.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.1, options: UIViewAnimationOptions.curveEaseIn, animations: { () -> Void in
                    label.alpha = 0
                },  completion: {
                    (value: Bool) in
                    label.removeFromSuperview()
                })
            })
        }
    
        class func showPositiveMessage(message:String)
        {
            showAlert(backgroundColor: UIColor.green, textColor: UIColor.white, message: message)
        }
        class func showNegativeMessage(message:String)
        {
            showAlert(backgroundColor: UIColor.red, textColor: UIColor.white, message: message)
        }
    }
    

提交回复
热议问题