0

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 :(

Dave Lee
  • 205
  • 5
  • 14
  • What exactly are you trying to do? You would never set all those six constraints on any view, so the question as it stands makes no sense. Show some existing code and we can talk about whether it can be shortened. Also note that there are third-party libraries that abbreviate the construction of constraints, which may be what you're thinking of (but we can't really talk about that here). – matt Apr 17 '18 at 22:55
  • 1
    @matt i recall the function took all six as parameters but does not require you to set every one of them. Also --please correct me if i'm wrong-- there are instances where you do set all six constraints (eg setting the content view within a scroll view) – Dave Lee Apr 18 '18 at 02:11
  • That's perfectly true! The content view would need to be pinned to the scroll view on all four sides _and_ have a height and width. — Anyway that does sound like a third-party thing. There's no such single command built into iOS Cocoa. – matt Apr 18 '18 at 03:57
  • 1
    You are probably thinking of SnapKit. http://snapkit.io/docs/ `make.edges.equalTo(superview)` kaboom – matt Apr 18 '18 at 03:59
  • Use “CGPoint” to change the position Or “CGRect” for position and dimensions” Check this thread: https://stackoverflow.com/a/46265201/5356650 – Simple Apr 17 '18 at 23:31

2 Answers2

0

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.

Mark
  • 515
  • 2
  • 4
  • 8
0

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
}
Jacob Lange
  • 1,299
  • 14
  • 22