Skip to content

Instantly share code, notes, and snippets.

@rajohns08
Last active September 28, 2016 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajohns08/ead5f6c92fa5d1c4b89fd08c125890e9 to your computer and use it in GitHub Desktop.
Save rajohns08/ead5f6c92fa5d1c4b89fd08c125890e9 to your computer and use it in GitHub Desktop.
iOS / Swift - Autolayout wrapper to reduce boilerplate
extension UIView {
var differentSuperviewsWarningMessage: String {
return "Since you are adding a constraint to self.superview, self and the view passed in need to have the same superview. The views you are trying to align do not have the same superview."
}
func centerInSuperview() {
self.centerVerticallyInSuperview()
self.centerHorizontallyInSuperview()
}
func centerVerticallyInSuperview() {
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .centerY,
relatedBy: .equal,
toItem: self.superview,
attribute: .centerY,
multiplier: 1,
constant: 0)
)
}
func centerHorizontallyInSuperview() {
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .centerX,
relatedBy: .equal,
toItem: self.superview,
attribute: .centerX,
multiplier: 1,
constant: 0)
)
}
func leadingSpaceToSuperview(space: CGFloat) {
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .leading,
relatedBy: .equal,
toItem: self.superview,
attribute: .leading,
multiplier: 1,
constant: space)
)
}
func trailingSpaceToSuperview(space: CGFloat) {
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .trailing,
relatedBy: .equal,
toItem: self.superview,
attribute: .trailing,
multiplier: 1,
constant: -space)
)
}
func topSpaceToSuperview(space: CGFloat) {
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .top,
relatedBy: .equal,
toItem: self.superview,
attribute: .top,
multiplier: 1,
constant: space)
)
}
func bottomSpaceToSuperview(space: CGFloat) {
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .bottom,
relatedBy: .equal,
toItem: self.superview,
attribute: .bottom,
multiplier: 1,
constant: -space))
}
func heightConstraint(height: CGFloat) {
self.addConstraint(NSLayoutConstraint(
item: self,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: height)
)
}
func widthConstraint(width: CGFloat) {
self.addConstraint(NSLayoutConstraint(
item: self,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1,
constant: width)
)
}
func verticalSpacing(below view: UIView, space: CGFloat) {
assert(self.superview == view.superview, differentSuperviewsWarningMessage)
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .top,
relatedBy: .equal,
toItem: view,
attribute: .bottom,
multiplier: 1,
constant: space)
)
}
func verticalSpacing(above view: UIView, space: CGFloat) {
assert(self.superview == view.superview, differentSuperviewsWarningMessage)
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .bottom,
relatedBy: .equal,
toItem: view,
attribute: .top,
multiplier: 1,
constant: -space)
)
}
func horizontalSpacing(toRightOf view: UIView, space: CGFloat) {
assert(self.superview == view.superview, differentSuperviewsWarningMessage)
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .left,
relatedBy: .equal,
toItem: view,
attribute: .right,
multiplier: 1,
constant: space)
)
}
func horizontalSpacing(toLeftOf view: UIView, space: CGFloat) {
assert(self.superview == view.superview, differentSuperviewsWarningMessage)
self.superview?.addConstraint(NSLayoutConstraint(
item: self,
attribute: .right,
relatedBy: .equal,
toItem: view,
attribute: .left,
multiplier: 1,
constant: -space)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment