Advertisement
Guest User

Extensions for manually adding UIView constraints

a guest
Apr 25th, 2017
407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.59 KB | None | 0 0
  1. extension UIView {
  2.     func addConstraintsWithFormat(_ format: String, views: UIView...) {
  3.         var viewsDictionary = [String: UIView]()
  4.        
  5.         for (index, view) in views.enumerated() {
  6.             let key = "v\(index)"
  7.             view.translatesAutoresizingMaskIntoConstraints = false
  8.             viewsDictionary[key] = view
  9.         }
  10.         addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
  11.     }
  12.    
  13.     func centerHorizontallyWithOffset(_ subview: UIView, offset: CGFloat) {
  14.         addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: offset))
  15.     }
  16.    
  17.     // center subview horizontally in view
  18.     func centerHorizontally(_ subview: UIView) {
  19.         addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0))
  20.     }
  21.     // combine with centerhorizontally and simply add an optional parameter
  22.     func centerVerticallyWithOffset(_ subview: UIView, offset: CGFloat) {
  23.         addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: offset))
  24.     }
  25.    
  26.     // center subview vertically in view
  27.     func centerVertically(_ subview: UIView, multiplier: CGFloat = 1.0, offset: CGFloat = 0.0) {
  28.         // from https://autolayoutconstraints.com/
  29.         addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: multiplier, constant: offset))
  30.     }
  31.    
  32.     func unCenterVertically(_ subview: UIView) {
  33.         removeConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))
  34.     }
  35.    
  36.     // add constraint for width to equal height
  37.     func makeSquare(_ subview: UIView) {
  38.         addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: subview, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0.0))
  39.     }
  40.    
  41.     // make a subview a percentage from the top of the superview (this)
  42.     func percentFromTop(_ subview: UIView, percent: CGFloat, constant: CGFloat = 0) {
  43.         addConstraint(NSLayoutConstraint(item: subview, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal,
  44.             toItem: self, attribute: NSLayoutAttribute.bottom, multiplier: percent, constant: constant))
  45.     }
  46.    
  47.     // constrain a child view to its sibling based on the distance between the top of the child to the bottom of the sibling
  48.     func percentFromSibling(_ child: UIView, sibling: UIView, percent: CGFloat, constant: CGFloat = 0) {
  49.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal,
  50.             toItem: sibling, attribute: NSLayoutAttribute.bottom, multiplier: percent, constant: constant))
  51.     }
  52.    
  53.     func widthPercentOfParent(_ child: UIView, percent: CGFloat) {
  54.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: percent, constant: 0.0))
  55.     }
  56.    
  57.     func heightPercentOfParent(_ child: UIView, percent: CGFloat) {
  58.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.height, multiplier: percent, constant: 0.0))
  59.     }
  60.    
  61.     func widthPercentOfSibling(_ child: UIView, sibling: UIView, percent: CGFloat, offset: CGFloat = 0) {
  62.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.width, multiplier: percent, constant: offset))
  63.     }
  64.    
  65.     func heightPercentOfSibling(_ child: UIView, sibling: UIView, percent: CGFloat, offset: CGFloat = 0) {
  66.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.height, multiplier: percent, constant: offset))
  67.     }
  68.    
  69.     func centerChildVerticallyToSibling(_ child: UIView, sibling: UIView, offset: CGFloat = 0) {
  70.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: offset))
  71.     }
  72.    
  73.     func centerChildHorizontallyToSibling(_ child: UIView, sibling: UIView, offset: CGFloat = 0) {
  74.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: offset))
  75.     }
  76.    
  77.     func centerChildToSibling(_ child: UIView, sibling: UIView) {
  78.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerX, multiplier: 1.0, constant: 0.0))
  79.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.centerY, multiplier: 1.0, constant: 0.0))
  80.     }
  81.    
  82.     func spaceVerticallyFromSibling(_ child: UIView, sibling: UIView, constant: CGFloat = 20, percent: CGFloat = 1) {
  83.         addConstraint(NSLayoutConstraint(item: child, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: sibling, attribute: NSLayoutAttribute.bottom, multiplier: percent, constant: constant))
  84.     }
  85.    
  86.     func fillAndCenter(_ child: UIView) {
  87.         addConstraintsWithFormat("V:|[v0]|", views: child)
  88.         addConstraintsWithFormat("H:|[v0]|", views: child)
  89.     }
  90.    
  91.     func makeRound() {
  92.         layer.cornerRadius = frame.width / 2
  93.         clipsToBounds = true
  94.         //layer.masksToBounds = true
  95.     }
  96.    
  97.     // add multiple subviews at once
  98.     func addSubviews(_ views: UIView...) {
  99.         for (_, view) in views.enumerated() {
  100.             addSubview(view)
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement