Created
August 23, 2016 22:25
-
-
Save antonio081014/abda44119f434db924769eb18270a1a9 to your computer and use it in GitHub Desktop.
Constrain Helper functions
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 { | |
public func constrainEqual(attribute: NSLayoutAttribute, to: AnyObject, multiplier: CGFloat = 1, constant: CGFloat = 0) { | |
constrainEqual(attribute, to: to, attribute, multiplier: multiplier, constant: constant) | |
} | |
public func constrainEqual(attribute: NSLayoutAttribute, to: AnyObject, _ toAttribute: NSLayoutAttribute, multiplier: CGFloat = 1, constant: CGFloat = 0) { | |
NSLayoutConstraint.activateConstraints([ | |
NSLayoutConstraint(item: self, attribute: attribute, relatedBy: .Equal, toItem: to, attribute: toAttribute, multiplier: multiplier, constant: constant) | |
] | |
) | |
} | |
public func constrainEdges(toMarginOf view: UIView) { | |
constrainEqual(.Top, to: view, .TopMargin) | |
constrainEqual(.Leading, to: view, .LeadingMargin) | |
constrainEqual(.Trailing, to: view, .TrailingMargin) | |
constrainEqual(.Bottom, to: view, .BottomMargin) | |
} | |
public func center(inView view: UIView) { | |
centerXAnchor.constrainEqual(view.centerXAnchor) | |
centerYAnchor.constrainEqual(view.centerYAnchor) | |
} | |
} | |
extension NSLayoutAnchor { | |
public func constrainEqual(anchor: NSLayoutAnchor, constant: CGFloat = 0) { | |
let constraint = constraintEqualToAnchor(anchor, constant: constant) | |
constraint.active = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment