Is there a way to set left, right, top, bottom, width, height autolayout constraints in a single line in swift?
I recall seeing this a few months back but could not find it in their documentation :(
Is there a way to set left, right, top, bottom, width, height autolayout constraints in a single line in swift?
I recall seeing this a few months back but could not find it in their documentation :(
Firstly you NEVER need to set 6 AutoLayout Constraints. You need exactly FOUR.
If you have set the LEFT and the RIGHT, then you DONT need the WIDTH and if you have set the TOP and the BOTTOM, then you DONT need the HEIGHT.
You can call NSLayoutConstraint, which takes 7 Parameters but you need to call it 4 times and then add them to the view.
rbTop = NSLayoutConstraint(item: requestButton, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 30)
You can also directly set the constraints by the anchors
requestView.widthAnchor.constraint(equalToConstant: view.bounds.width).isActive = true which is also a nice way of doing it, but again you need to set 4 constraints.
You might be thinking about these two UIView Extension methods? Credit to Brian Voong
You could easily modify this to your needs if you want to keep it simple and not use a third party lib but still want to make life easier when working with constraints programmatically.
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) {
translatesAutoresizingMaskIntoConstraints = false
_ = anchorWithReturnAnchors(top, left: left, bottom: bottom, right: right, topConstant: topConstant, leftConstant: leftConstant, bottomConstant: bottomConstant, rightConstant: rightConstant, widthConstant: widthConstant, heightConstant: heightConstant)
}
public func anchorWithReturnAnchors(_ 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
}