-
-
Save shsteven/f68676113d5d46eacb7c95d818add99b 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
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") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for Swift 5.