Reputation: 739
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.
Upvotes: 2
Views: 958
Reputation: 4333
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.
Upvotes: 1
Reputation: 508
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.
Upvotes: 0