Skip to content

Instantly share code, notes, and snippets.

@danielgehr
Last active January 12, 2018 08:58
Show Gist options
  • Save danielgehr/34922dd1299cf122c8ddf42d91fc9d64 to your computer and use it in GitHub Desktop.
Save danielgehr/34922dd1299cf122c8ddf42d91fc9d64 to your computer and use it in GitHub Desktop.
UIView Swift extension to add auto layout constraints to the view
import UIKit
public extension UIView {
func addHeightConstraint(_ height: CGFloat) {
addConstraint(NSLayoutConstraint(item: self,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: height))
}
func addHeightWidthRatio( _ ratio: CGFloat) {
addConstraint(NSLayoutConstraint(item: self,
attribute: .height,
relatedBy: .equal,
toItem: self,
attribute: .width,
multiplier: ratio, constant: 0))
}
func addWidthConstraint(_ height: CGFloat) {
addConstraint(NSLayoutConstraint(item: self,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: height))
}
func centerXAxisToSuperView() {
guard let superview = superview else { return }
superview.addConstraint(NSLayoutConstraint(item: self,
attribute: .centerX,
relatedBy: .equal,
toItem: superview,
attribute: .centerX,
multiplier: 1,
constant: 0))
}
func centerYAxisToSuperView() {
guard let superview = superview else { return }
superview.addConstraint(NSLayoutConstraint(item: self,
attribute: .centerY,
relatedBy: .equal,
toItem: superview,
attribute: .centerY,
multiplier: 1,
constant: 0))
}
func centerToSuperView() {
centerXAxisToSuperView()
centerYAxisToSuperView()
}
func stickEdgesToSuperView(edges: [NSLayoutAttribute] = [.leading, .trailing, .top, .bottom], spaces: [CGFloat] = [0.0, 0.0, 0.0, 0.0]) {
guard let superview = superview, edges.count == spaces.count else { return }
for (index, edge) in edges.enumerated() {
switch edge {
case .top, .leading:
superview.addConstraint(NSLayoutConstraint(item: self,
attribute: edge,
relatedBy: .equal,
toItem: superview,
attribute: edge,
multiplier: 1.0,
constant: spaces[index]))
case .bottom, .trailing:
superview.addConstraint(NSLayoutConstraint(item: superview,
attribute: edge,
relatedBy: .equal,
toItem: self,
attribute: edge,
multiplier: 1.0,
constant: spaces[index]))
default:
break
}
}
}
func stickToTopView(topView: UIView, _ space: CGFloat) {
guard let superview = superview else { return }
superview.addConstraint(NSLayoutConstraint(
item: self,
attribute: .top,
relatedBy: .equal,
toItem: topView,
attribute: topView == superview ? .top : .bottom,
multiplier: 1.0,
constant: space))
}
func stickToBottomView(bottomView: UIView, _ space: CGFloat) {
guard let superview = superview else { return }
superview.addConstraint(NSLayoutConstraint(
item: bottomView,
attribute: bottomView == superview ? .bottom : .top,
relatedBy: .equal,
toItem: self,
attribute: .bottom,
multiplier: 1.0,
constant: space))
}
func stickToLeftView(leftView: UIView, _ space: CGFloat) {
guard let superview = superview else { return }
superview.addConstraint(NSLayoutConstraint(
item: self,
attribute: .leading,
relatedBy: .equal,
toItem: leftView,
attribute: leftView == superview ? .leading : .trailing,
multiplier: 1.0,
constant: space))
}
func stickToRightView(rightView: UIView, _ space: CGFloat) {
guard let superview = superview else { return }
superview.addConstraint(NSLayoutConstraint(
item: rightView,
attribute: rightView == superview ? .trailing : .leading,
relatedBy: .equal,
toItem: self,
attribute: .trailing,
multiplier: 1.0,
constant: space))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment