Last active
September 28, 2016 00:44
-
-
Save rajohns08/ead5f6c92fa5d1c4b89fd08c125890e9 to your computer and use it in GitHub Desktop.
iOS / Swift - Autolayout wrapper to reduce boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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