Last active
March 24, 2021 15:47
-
-
Save danielgalasko/c645434dc60cb895a557 to your computer and use it in GitHub Desktop.
A UIView extension for common auto layout constraints
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 { | |
func addConstaintsToPinHorizontalEdgesToSuperView(padding: CGFloat = 0) { | |
prepareForConstraints() | |
self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(padding)-[view]-(padding)-|", | |
options: NSLayoutFormatOptions(0), | |
metrics: ["padding":padding], | |
views: ["view":self])) | |
} | |
func addConstaintsToPinVerticalEdgesToSuperView(padding: CGFloat = 0) { | |
prepareForConstraints() | |
self.superview!.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(padding)-[view]-(padding)-|", | |
options: NSLayoutFormatOptions(0), | |
metrics: ["padding":padding], | |
views: ["view":self])) | |
} | |
func addConstaintsToCenterVertically() { | |
prepareForConstraints() | |
self.superview!.addConstraint(NSLayoutConstraint(item: self, | |
attribute: .CenterY, | |
relatedBy: .Equal, | |
toItem: self.superview!, | |
attribute: .CenterY, | |
multiplier: 1.0, constant: 0)) | |
} | |
func addConstaintsToCenterHorizontally() { | |
prepareForConstraints() | |
self.superview!.addConstraint(NSLayoutConstraint(item: self, | |
attribute: .CenterX, | |
relatedBy: .Equal, | |
toItem: self.superview!, | |
attribute: .CenterX, | |
multiplier: 1.0, constant: 0)) | |
} | |
func addConstaintsToPinLeadingToSuperview(constant: CGFloat) -> NSLayoutConstraint { | |
prepareForConstraints() | |
let constraint = NSLayoutConstraint(item: self, | |
attribute: .Leading, | |
relatedBy: .Equal, | |
toItem: self.superview!, | |
attribute: .Leading, | |
multiplier: 1, constant: constant) | |
self.superview!.addConstraint(constraint) | |
return constraint | |
} | |
func addConstaintsToPinTrailingToSuperview(constant: CGFloat) -> NSLayoutConstraint { | |
prepareForConstraints() | |
let constraint = NSLayoutConstraint(item: self, | |
attribute: .Trailing, | |
relatedBy: .Equal, | |
toItem: self.superview!, | |
attribute: .Trailing, | |
multiplier: 1, constant: constant) | |
self.superview!.addConstraint(constraint) | |
return constraint | |
} | |
func addConstaintsToPinTopToSuperview(constant: CGFloat) -> NSLayoutConstraint { | |
prepareForConstraints() | |
let constraint = NSLayoutConstraint(item: self, | |
attribute: .Top, | |
relatedBy: .Equal, | |
toItem: self.superview!, | |
attribute: .Top, | |
multiplier: 1, constant: constant) | |
self.superview!.addConstraint(constraint) | |
return constraint | |
} | |
func addConstaintsToPinTopToView(view:UIView,constant: CGFloat) -> NSLayoutConstraint { | |
prepareForConstraints() | |
let constraint = NSLayoutConstraint(item: self, | |
attribute: .Top, | |
relatedBy: .Equal, | |
toItem: view, | |
attribute: .Top, | |
multiplier: 1, constant: constant) | |
self.superview!.addConstraint(constraint) | |
return constraint | |
} | |
func addConstaintsToPinBottomToView(view:UIView,constant: CGFloat) -> NSLayoutConstraint { | |
prepareForConstraints() | |
let constraint = NSLayoutConstraint(item: self, | |
attribute: .Bottom, | |
relatedBy: .Equal, | |
toItem: view, | |
attribute: .Bottom, | |
multiplier: 1, constant: constant) | |
self.superview!.addConstraint(constraint) | |
return constraint | |
} | |
func addConstaintsToFillSuperviewWithPadding(padding: CGFloat = 0) { | |
prepareForConstraints() | |
addConstaintsToPinHorizontalEdgesToSuperView(padding: padding) | |
addConstaintsToPinVerticalEdgesToSuperView(padding: padding) | |
} | |
private func prepareForConstraints() { | |
self.setTranslatesAutoresizingMaskIntoConstraints(false) | |
if superview == nil { | |
assert(false, "You need to have a superview before you can add contraints") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
swift 4.2 update + some refactors to drop force unwraps + accessing functionality through
view.layout. pinHorizontalEdgesToSuperView()
for example + some smallish refactors: