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
Instead of using UILabel
using UITextView
gets better results.
func showToast(message: String) {
let toastLabel = UITextView(frame: CGRect(x: self.view.frame.size.width/16, y: self.view.frame.size.height-150, width: self.view.frame.size.width * 7/8, height: 35))
toastLabel.backgroundColor = UIColor.black.withAlphaComponent(0.6)
toastLabel.textColor = UIColor.white
toastLabel.textAlignment = .center;
toastLabel.text = " \(message) "
toastLabel.alpha = 1.0
toastLabel.layer.cornerRadius = 10;
toastLabel.clipsToBounds = true
toastLabel.font = UIFont(name: (toastLabel.font?.fontName)!, size: 16)
toastLabel.layoutEdgeInsets.left = 8
toastLabel.layoutEdgeInsets.right = 8
toastLabel.center.x = self.view.frame.size.width/2
self.view.addSubview(toastLabel)
UIView.animate(withDuration: 5.0, delay: 0.1, options: .curveEaseOut, animations: {
toastLabel.alpha = 0.0
}, completion: {(isCompleted) in
toastLabel.removeFromSuperview()
})
}
Space is added with message to provide good spacing at the both ends so that it looks good. Modified version of answer of Mr.Bean