Skip to content

Instantly share code, notes, and snippets.

@tanabee
Last active May 20, 2016 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanabee/447835d9ef80fc749ee6871e2d2d4462 to your computer and use it in GitHub Desktop.
Save tanabee/447835d9ef80fc749ee6871e2d2d4462 to your computer and use it in GitHub Desktop.
親ビューに対して上下左右マージンゼロで制約指定するコード(WKWebView とかで使う)
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