Last active
May 20, 2016 03:46
-
-
Save tanabee/447835d9ef80fc749ee6871e2d2d4462 to your computer and use it in GitHub Desktop.
親ビューに対して上下左右マージンゼロで制約指定するコード(WKWebView とかで使う)
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
extension UIView { | |
// 親ビュー (parent) に対して上下左右マージンゼロの制約を指定をする | |
func applyAutoLayoutMatchParent(parent: UIView, margin: CGFloat = 0) { | |
self.translatesAutoresizingMaskIntoConstraints = false | |
let attributes: [NSLayoutAttribute] = [.Top, .Left, .Right, .Bottom] | |
let constraints = attributes.map { (attribute) -> NSLayoutConstraint in | |
return NSLayoutConstraint( | |
item: self, | |
attribute: attribute, | |
relatedBy: .Equal, | |
toItem: parent, | |
attribute: attribute, | |
multiplier: 1.0, | |
constant: margin | |
) | |
} | |
parent.addConstraints(constraints) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment