Thursday, February 21, 2019

Toast in Swift

Swift doesn’t has toast, if want a toast like Android, we create a function like this.
func toast(t:String){
        let toastLabel = UILabel(frame: CGRectMake(view.frame.size.width/2 - 150, 150, 300, 35))
        toastLabel.backgroundColor = UIColor.blackColor()
        toastLabel.textColor = UIColor.whiteColor()
        toastLabel.textAlignment = NSTextAlignment.Center;
        self.view.addSubview(toastLabel)
        toastLabel.text = t
        toastLabel.alpha = 1.0
        toastLabel.layer.cornerRadius = 10;
        toastLabel.clipsToBounds  =  true
        UIView.animateWithDuration(4.0, delay: 0.5, options: .CurveEaseOut, animations: {
           
            toastLabel.alpha = 0.0
           
            }, completion: nil)
    }
When want to show toast, call it like this.

toast("Here is a toast like Android")

No comments:

Post a Comment