Guest User

Untitled

a guest
Jan 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. extension UIViewController {
  2. func showToast(message : String) {
  3. guard let window = UIApplication.shared.keyWindow else {return}
  4. let messageLbl = UILabel()
  5. messageLbl.text = message
  6. messageLbl.textAlignment = .center
  7. messageLbl.font = UIFont.systemFont(ofSize: 12)
  8. messageLbl.textColor = .white
  9. messageLbl.backgroundColor = UIColor(white: 0, alpha: 0.5)
  10.  
  11. let textSize:CGSize = messageLbl.intrinsicContentSize
  12. let labelWidth = min(textSize.width, window.frame.width - 40)
  13.  
  14. messageLbl.frame = CGRect(x: 20, y: window.frame.height - 90, width: labelWidth + 30, height: textSize.height + 20)
  15. messageLbl.center.x = window.center.x
  16. messageLbl.layer.cornerRadius = messageLbl.frame.height/2
  17. messageLbl.layer.masksToBounds = true
  18. window.addSubview(messageLbl)
  19.  
  20. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  21. UIView.animate(withDuration: 1, animations: {
  22. messageLbl.alpha = 0
  23. }) { (_) in
  24. messageLbl.removeFromSuperview()
  25. }
  26. }
  27. }
  28. }
Add Comment
Please, Sign In to add comment