Advertisement
rehannali

UIView extension to debloat a lot of code.

Dec 28th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.29 KB | None | 0 0
  1. struct AnchoredConstraints {
  2.     var top, leading, bottom, trailing, width, height: NSLayoutConstraint?
  3. }
  4.  
  5.  
  6. extension UIView {
  7.    
  8.     @discardableResult
  9.     func anchor(top: NSLayoutYAxisAnchor?, leading: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, size: CGSize = .zero) -> AnchoredConstraints {
  10.        
  11.         translatesAutoresizingMaskIntoConstraints = false
  12.         var anchoredConstraints = AnchoredConstraints()
  13.        
  14.         if let top = top {
  15.             anchoredConstraints.top = topAnchor.constraint(equalTo: top, constant: padding.top)
  16.         }
  17.        
  18.         if let leading = leading {
  19.             anchoredConstraints.leading = leadingAnchor.constraint(equalTo: leading, constant: padding.left)
  20.         }
  21.        
  22.         if let bottom = bottom {
  23.             anchoredConstraints.bottom = bottomAnchor.constraint(equalTo: bottom, constant: -padding.bottom)
  24.         }
  25.        
  26.         if let trailing = trailing {
  27.             anchoredConstraints.trailing = trailingAnchor.constraint(equalTo: trailing, constant: -padding.right)
  28.         }
  29.        
  30.         if size.width != 0 {
  31.             anchoredConstraints.width = widthAnchor.constraint(equalToConstant: size.width)
  32.         }
  33.        
  34.         if size.height != 0 {
  35.             anchoredConstraints.height = heightAnchor.constraint(equalToConstant: size.height)
  36.         }
  37.        
  38.         [anchoredConstraints.top, anchoredConstraints.leading, anchoredConstraints.bottom, anchoredConstraints.trailing, anchoredConstraints.width, anchoredConstraints.height].forEach{ $0?.isActive = true }
  39.        
  40.         return anchoredConstraints
  41.     }
  42.    
  43.     func fillSuperview(padding: UIEdgeInsets = .zero) {
  44.         translatesAutoresizingMaskIntoConstraints = false
  45.         if let superviewTopAnchor = superview?.topAnchor {
  46.             topAnchor.constraint(equalTo: superviewTopAnchor, constant: padding.top).isActive = true
  47.         }
  48.        
  49.         if let superviewBottomAnchor = superview?.bottomAnchor {
  50.             bottomAnchor.constraint(equalTo: superviewBottomAnchor, constant: -padding.bottom).isActive = true
  51.         }
  52.        
  53.         if let superviewLeadingAnchor = superview?.leadingAnchor {
  54.             leadingAnchor.constraint(equalTo: superviewLeadingAnchor, constant: padding.left).isActive = true
  55.         }
  56.        
  57.         if let superviewTrailingAnchor = superview?.trailingAnchor {
  58.             trailingAnchor.constraint(equalTo: superviewTrailingAnchor, constant: -padding.right).isActive = true
  59.         }
  60.     }
  61.    
  62.     func centerInSuperview(size: CGSize = .zero) {
  63.         translatesAutoresizingMaskIntoConstraints = false
  64.         if let superviewCenterXAnchor = superview?.centerXAnchor {
  65.             centerXAnchor.constraint(equalTo: superviewCenterXAnchor).isActive = true
  66.         }
  67.        
  68.         if let superviewCenterYAnchor = superview?.centerYAnchor {
  69.             centerYAnchor.constraint(equalTo: superviewCenterYAnchor).isActive = true
  70.         }
  71.        
  72.         if size.width != 0 {
  73.             widthAnchor.constraint(equalToConstant: size.width).isActive = true
  74.         }
  75.        
  76.         if size.height != 0 {
  77.             heightAnchor.constraint(equalToConstant: size.height).isActive = true
  78.         }
  79.     }
  80.    
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement