Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.60 KB | None | 0 0
  1. extension UIViewController {
  2.    
  3.     func showToast(message: String) {
  4.         let toastContainer = UIView(frame: CGRect())
  5.         toastContainer.backgroundColor = UIColor.black.withAlphaComponent(0.6)
  6.         toastContainer.alpha = 0.0
  7.         toastContainer.layer.cornerRadius = 25
  8.         toastContainer.clipsToBounds  =  true
  9.        
  10.         let toastLabel = UILabel(frame: CGRect())
  11.         toastLabel.textColor = UIColor.white
  12.         toastLabel.textAlignment = .center
  13.         toastLabel.font.withSize(12.0)
  14.         toastLabel.text = message
  15.         toastLabel.clipsToBounds  =  true
  16.         toastLabel.numberOfLines = 0
  17.        
  18.         toastContainer.addSubview(toastLabel)
  19.         self.view.addSubview(toastContainer)
  20.        
  21.         toastLabel.translatesAutoresizingMaskIntoConstraints = false
  22.         toastContainer.translatesAutoresizingMaskIntoConstraints = false
  23.        
  24.         let a1 = NSLayoutConstraint(item: toastLabel, attribute: .leading, relatedBy: .equal, toItem: toastContainer, attribute: .leading, multiplier: 1, constant: 15)
  25.         let a2 = NSLayoutConstraint(item: toastLabel, attribute: .trailing, relatedBy: .equal, toItem: toastContainer, attribute: .trailing, multiplier: 1, constant: -15)
  26.         let a3 = NSLayoutConstraint(item: toastLabel, attribute: .bottom, relatedBy: .equal, toItem: toastContainer, attribute: .bottom, multiplier: 1, constant: -15)
  27.         let a4 = NSLayoutConstraint(item: toastLabel, attribute: .top, relatedBy: .equal, toItem: toastContainer, attribute: .top, multiplier: 1, constant: 15)
  28.         toastContainer.addConstraints([a1, a2, a3, a4])
  29.        
  30.         let c1 = NSLayoutConstraint(item: toastContainer, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 65)
  31.         let c2 = NSLayoutConstraint(item: toastContainer, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: -65)
  32.         let c3 = NSLayoutConstraint(item: toastContainer, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -75)
  33.         self.view.addConstraints([c1, c2, c3])
  34.        
  35.         UIView.animate(withDuration: 0.5, delay: 0.0, options: .curveEaseIn, animations: {
  36.             toastContainer.alpha = 1.0
  37.         }, completion: { _ in
  38.             UIView.animate(withDuration: 0.5, delay: 2, options: .curveEaseOut, animations: {
  39.                 toastContainer.alpha = 0.0
  40.             }, completion: {_ in
  41.                 toastContainer.removeFromSuperview()
  42.                
  43.             })
  44.         })
  45.         }
  46.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement