2

Is there any way to share constants between constraints in a storyboard (or better yet define them globally and use when needed)?

Say, I want a distance from top to be equal to the distance from the left - those are not achievable by setting any sort of symmetry, and I don't want to change both of them every single time, just change one and see the result.

I want a storyboard (clickable) solution, no coding required.

Antek
  • 721
  • 1
  • 4
  • 27
  • Please be more specific about your requirements. – Arun Kumar Feb 14 '18 at 10:48
  • 2
    edited. When downvoting, please state your reason though - otherwise I won't learn anything from the process. – Antek Feb 14 '18 at 10:54
  • 1
    Can you show us an example of what you tried, and why this does not work? – Dominique Feb 14 '18 at 10:56
  • well, so far I just browsed around the XCode UI, as I wanted a XIB solution, not coding any new class, as I wanted something generic and general – Antek Feb 14 '18 at 11:54
  • and of course I browsed google and SO extensively, but it seems to me more and more that SO's not the best place to look for iOS answers – Antek Feb 14 '18 at 11:54

2 Answers2

1

Well, you can't use defined constants in storyboard, but it is possible to share properties. Depending on how complex your design is it might not be worth the trouble. If you just want the same distance shared between two items you are probably better off editing their value. If you have several dependencies you can create "spacers" that share the same sizes.

It is quite simple, just add two or more (hidden) UIView objects to your storyboard. Choose one to be the master item, then set the others to have the size properties of that. The master can be set to have ratio 1:1, so that you only need to set and change the height of it to resize all of them in both X and Y. You then align your other items to these objects.

Also note that you can have other values than 1:1 for the multiplier.

Another note: if you want just one view to be positioned, one such hidden view will suffice.

enter image description here

Community
  • 1
  • 1
LGP
  • 4,135
  • 1
  • 22
  • 34
  • well, better yet I could then use just one hidden view and alter its width. That's still very much enlightening, as I didn't know it's impossible for constraints to share properties (if I get you right). – Antek Feb 14 '18 at 12:03
  • would you mind editing your question accordingly, so I could accept it? – Antek Feb 14 '18 at 12:17
  • Not sure I understand. What would you like me to change? – LGP Feb 14 '18 at 12:23
  • 1
    ahh... speaking not clearly enough from my side again, excuse me - I meant _me_ editing your question, but you could do this either. To your wish. – Antek Feb 14 '18 at 12:25
  • No problem, go ahead! – LGP Feb 14 '18 at 12:27
  • 1
    thank you. I just added the simplest case description that I sought after – Antek Feb 14 '18 at 12:31
0

It's hard to say without more info, but I think the best solution would be to subclass UIView, something like this:

SWIFT 4.0

class MySymmetricView {
    @IBInspectable var inset: Float = 10.0 // Put your constraints here, @IBInspectable will expose them to IB, you can also set your constants here

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupConstraints()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupConstraints()
    }

    func setupConstraints() {
         NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: superview, attribute: .leadingMargin, multiplier: 1.0, constant: inset).isActive = true
         NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: superview, attribute: .leadingMargin, multiplier: 1.0, constant: inset).isActive = true
    }

}

Or subclass the constraints themselves in a similar way.

  • that's not really generic and general (although I know I didn't specify it when you wrote your answer) – Antek Feb 14 '18 at 12:05