Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shsteven/f68676113d5d46eacb7c95d818add99b to your computer and use it in GitHub Desktop.
Save shsteven/f68676113d5d46eacb7c95d818add99b to your computer and use it in GitHub Desktop.
A UIView extension for common auto layout constraints
import UIKit
extension UIView {
func addConstaintsToPinHorizontalEdgesToSuperView(padding: CGFloat = 0) {
prepareForConstraints()
self.superview!.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-(padding)-[view]-(padding)-|",
options: [],
metrics: ["padding":padding],
views: ["view":self]))
}
func addConstaintsToPinVerticalEdgesToSuperView(padding: CGFloat = 0) {
prepareForConstraints()
self.superview!.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-(padding)-[view]-(padding)-|",
options: [],
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.translatesAutoresizingMaskIntoConstraints = false
if superview == nil {
assert(false, "You need to have a superview before you can add contraints")
}
}
}
@shsteven
Copy link
Author

Updated for Swift 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment