Last active
November 27, 2017 14:34
-
-
Save SwiftTsubame/df2b43b0531d17fdb5d0cfc3dd61e845 to your computer and use it in GitHub Desktop.
UIView Layout Anchors
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
// Inspired by UIView Anchors from Brian Voong. Please check out his work at https://github.com/bhlvoong/LBTAComponents | |
extension UIView { | |
public func addSubViewList(_ view: UIView...) { | |
view.forEach { self.addSubview($0) } | |
} | |
public func fillSuperview() { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let superview = superview { | |
leftAnchor.constraint(equalTo: superview.leftAnchor).isActive = true | |
rightAnchor.constraint(equalTo: superview.rightAnchor).isActive = true | |
topAnchor.constraint(equalTo: superview.topAnchor).isActive = true | |
bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true | |
} | |
} | |
public func anchorCenterXToSuperview(constant: CGFloat = 0) { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let anchor = superview?.centerXAnchor { | |
centerXAnchor.constraint(equalTo: anchor, constant: constant).isActive = true | |
} | |
} | |
public func anchorCenterYToSuperview(constant: CGFloat = 0) { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let anchor = superview?.centerYAnchor { | |
centerYAnchor.constraint(equalTo: anchor, constant: constant).isActive = true | |
} | |
} | |
public func anchorCenterSuperview() { | |
anchorCenterXToSuperview() | |
anchorCenterYToSuperview() | |
} | |
public func equalWidthToHeight() { | |
translatesAutoresizingMaskIntoConstraints = false | |
widthAnchor.constraint(equalTo: heightAnchor, multiplier: 1.0).isActive = true | |
} | |
public func equalHeightToWidth() { | |
translatesAutoresizingMaskIntoConstraints = false | |
heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1.0).isActive = true | |
} | |
public func proportionWidthToSuperView(_ multiplier: CGFloat) { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let anchor = superview?.widthAnchor { | |
widthAnchor.constraint(equalTo: anchor, multiplier: multiplier).isActive = true | |
} | |
} | |
public func proportionHeightToSuperView(_ multiplier: CGFloat) { | |
translatesAutoresizingMaskIntoConstraints = false | |
if let anchor = superview?.heightAnchor { | |
heightAnchor.constraint(equalTo: anchor, multiplier: multiplier).isActive = true | |
} | |
} | |
public func proportionWidthAndHeightToSuperView(widthMultiplier: CGFloat, heightMultiplier: CGFloat) { | |
translatesAutoresizingMaskIntoConstraints = false | |
proportionHeightToSuperView(heightMultiplier) | |
proportionWidthToSuperView(widthMultiplier) | |
} | |
public func anchorToTop(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil) { | |
anchorWithConstantsToTop(top, left: left, bottom: bottom, right: right, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0) | |
} | |
public func anchorWithConstantsToTop(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0) { | |
_ = anchor(top: top, left: left, bottom: bottom, right: right, topConstant: topConstant, leftConstant: leftConstant, bottomConstant: bottomConstant, rightConstant: rightConstant) | |
} | |
public func anchor(top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] { | |
translatesAutoresizingMaskIntoConstraints = false | |
var anchors = [NSLayoutConstraint]() | |
if let top = top { | |
anchors.append(topAnchor.constraint(equalTo: top, constant: topConstant)) | |
} | |
if let left = left { | |
anchors.append(leftAnchor.constraint(equalTo: left, constant: leftConstant)) | |
} | |
if let bottom = bottom { | |
anchors.append(bottomAnchor.constraint(equalTo: bottom, constant: -bottomConstant)) | |
} | |
if let right = right { | |
anchors.append(rightAnchor.constraint(equalTo: right, constant: -rightConstant)) | |
} | |
if widthConstant > 0 { | |
anchors.append(widthAnchor.constraint(equalToConstant: widthConstant)) | |
} | |
if heightConstant > 0 { | |
anchors.append(heightAnchor.constraint(equalToConstant: heightConstant)) | |
} | |
anchors.forEach({$0.isActive = true}) | |
return anchors | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment