Skip to content

Instantly share code, notes, and snippets.

@drinkius
Forked from danielgalasko/UIView+AutoLayout.swift
Created November 20, 2018 11:48
Show Gist options
  • Select an option

  • Save drinkius/ba554d496c9af93cb6b2a9bde70d41ef to your computer and use it in GitHub Desktop.

Select an option

Save drinkius/ba554d496c9af93cb6b2a9bde70d41ef to your computer and use it in GitHub Desktop.
A UIView extension for common auto layout constraints
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