Make your own toast view in Swift
Suppose you want to show the message for some time after that that message will hide automatically. To show the message we use UIAlertView, but this is not the best way. So in iOS by default don’t have a predefined toast view. In Toast View you can define the time interval that how much time the toast view will visible on your view. You can define in global class of your project . you can call this :-
1 |
GlobalData.toastView(messsage: "YOUR MESSAGE", view: self.view) |
GlobalData = Class where your method is define.
message = you want to show in ToastView
view = parent view of your current view i.e. (self.view)
ToastView Method :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class func toastView(messsage : String, view: UIView ){ let toastLabel = UILabel(frame: CGRect(x: view.frame.size.width/2 - 150, y: view.frame.size.height-100, width: 300, height : 35)) toastLabel.backgroundColor = UIColor().HexToColor(hexString: primary_color) toastLabel.textColor = UIColor.white toastLabel.textAlignment = NSTextAlignment.center; view.addSubview(toastLabel) toastLabel.text = messsage toastLabel.alpha = 1.0 toastLabel.layer.cornerRadius = 10; toastLabel.clipsToBounds = true UIView.animate(withDuration: 4.0, delay: 0.1, options: UIViewAnimationOptions.curveEaseOut, animations: { toastLabel.alpha = 0.0 }) } |