Skip to content

Instantly share code, notes, and snippets.

@roop
Created July 10, 2015 12:58
Show Gist options
  • Save roop/7c91f8ebdfbdcb627c08 to your computer and use it in GitHub Desktop.
Save roop/7c91f8ebdfbdcb627c08 to your computer and use it in GitHub Desktop.
Helper for specifying AutoLayout constraints in code
/*
AutoLayoutHelper.swift is published under the MIT License.
Copyright (C) 2014-15, Roopesh Chander http://roopc.net/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import UIKit
enum LayoutInfo {
case FillIn(UIView)
case FillHorizontallyIn(UIView)
case FillVerticallyIn(UIView)
case CenterIn(UIView)
case CenterVerticallyIn(UIView)
case CenterHorizontallyIn(UIView)
case Anchor(NSLayoutAttribute, (ViewOrLayoutGuide, NSLayoutAttribute, Float))
}
enum ViewOrLayoutGuide {
case View(UIView)
case LayoutGuide(UILayoutSupport)
}
extension UIView {
func top(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.View(self), .Top, margin)
}
func bottom(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.View(self), .Bottom, margin)
}
func left(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.View(self), .Left, margin)
}
func right(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.View(self), .Right, margin)
}
func centerX(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.View(self), .CenterX, margin)
}
func centerY(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.View(self), .CenterY, margin)
}
}
extension UIViewController {
func bottomOfTopLayoutGuide(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.LayoutGuide(self.topLayoutGuide), .Bottom, margin)
}
func topOfBottomLayoutGuide(margin: Float = 0) -> (ViewOrLayoutGuide, NSLayoutAttribute, Float) {
return (ViewOrLayoutGuide.LayoutGuide(self.bottomLayoutGuide), .Top, margin)
}
}
let Required: UILayoutPriority = 1000 // UILayoutPriorityRequired is undefined in Swift.
extension UIView {
func addAsRequiredConstraint(constraint: NSLayoutConstraint) {
constraint.priority = Required
addConstraint(constraint)
}
func addSubviewsWithConstraints(subviews: (UIView, Array<LayoutInfo>)...) -> Void {
let currentSubviews: NSArray = self.subviews
iteratingOverSubviews: for subviewDataTuple in subviews {
let (subview, layoutInfoArray) = subviewDataTuple
if (currentSubviews.containsObject(subview)) {
continue iteratingOverSubviews
}
addSubview(subview)
subview.setTranslatesAutoresizingMaskIntoConstraints(false)
addLayoutConstraintsForSubview(subview, layoutInfoArray)
}
}
func addLayoutConstraintsForSubview(subview: UIView, _ layoutInfoArray: Array<LayoutInfo>) -> Void {
func bindLayoutAttributes(view1: UIView, view2: UIView, layoutAttributes: [NSLayoutAttribute]) {
for attribute in layoutAttributes {
addAsRequiredConstraint(NSLayoutConstraint(item: view1, attribute: attribute, relatedBy: .Equal,
toItem: view2, attribute: attribute, multiplier: 1, constant: 0))
}
}
for (layoutInfo: LayoutInfo) in layoutInfoArray {
switch layoutInfo {
case .FillIn(let view):
bindLayoutAttributes(subview, view, [.Top, .Right, .Bottom, .Left])
case .FillHorizontallyIn(let view):
bindLayoutAttributes(subview, view, [.Left, .Right])
case .FillVerticallyIn(let view):
bindLayoutAttributes(subview, view, [.Top, .Bottom])
case .CenterIn(let view):
bindLayoutAttributes(subview, view, [.CenterX, .CenterY])
case .CenterHorizontallyIn(let view):
bindLayoutAttributes(subview, view, [.CenterX])
case .CenterVerticallyIn(let view):
bindLayoutAttributes(subview, view, [.CenterY])
case .Anchor(let attr1, (let viewOrLayoutGuide, let attr2, let margin)):
switch viewOrLayoutGuide {
case .View(let v):
addAsRequiredConstraint(NSLayoutConstraint(item: subview, attribute: attr1, relatedBy: .Equal,
toItem: v, attribute: attr2, multiplier: 1, constant: CGFloat(margin)))
case .LayoutGuide(let l):
addAsRequiredConstraint(NSLayoutConstraint(item: subview, attribute: attr1, relatedBy: .Equal,
toItem: l, attribute: attr2, multiplier: 1, constant: CGFloat(margin)))
}
}
}
}
func removeLayoutConstraintsForSubview(subview: UIView) {
if subview.superview != self {
return
}
for c in self.constraints() {
if let constraint = c as? NSLayoutConstraint {
if ((constraint.firstItem as? UIView == subview && constraint.secondItem as? UIView == self) ||
(constraint.firstItem as? UIView == self && constraint.secondItem as? UIView == subview)) {
self.removeConstraint(constraint)
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment