Last active
July 25, 2018 11:01
-
-
Save tanner0101/3dcab950560aea7ebc8f to your computer and use it in GitHub Desktop.
Constrain a subview to all the edges of its superview with one line.
This file contains hidden or 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
/** | |
UIViewExtensions.swift | |
Constrain a subview to all the edges of its superview with one line. | |
<https://gist.github.com/3dcab950560aea7ebc8f.git> | |
*/ | |
import UIKit | |
extension UIView { | |
func constrainToSuperview(superview: UIView) { | |
self.setTranslatesAutoresizingMaskIntoConstraints(false) | |
//align to edges of super view | |
superview.addConstraint( | |
NSLayoutConstraint(item: self, attribute: .Top, relatedBy: .Equal, toItem: superview, attribute: .Top, multiplier: 1, constant: 0) | |
) | |
superview.addConstraint( | |
NSLayoutConstraint(item: self, attribute: .Right, relatedBy: .Equal, toItem: superview, attribute: .Right, multiplier: 1, constant: 0) | |
) | |
superview.addConstraint( | |
NSLayoutConstraint(item: self, attribute: .Bottom, relatedBy: .Equal, toItem: superview, attribute: .Bottom, multiplier: 1, constant: 0) | |
) | |
superview.addConstraint( | |
NSLayoutConstraint(item: self, attribute: .Left, relatedBy: .Equal, toItem: superview, attribute: .Left, multiplier: 1, constant: 0) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment