Auto-layout uses the -
symbol to indicate that a constraint should use the default spacing. Is there a way to set the priority on that spacing without having to explicitly set a metric value?
For example, the following visual format will create a leading constraint between myView
and its superview equal to the default Cocoa/CocoaTouch value. But that constraint has a priority of required
.
@"H:|-[myView]"
The following will create a leading constraint with a lower priority but an explicit metric value must be specified:
@"H:|-(10@750)-[myView]"
I want to be able to give the leading constraint a priority, but not an explicit metric. The reasoning being that 1) I don't want to have to guess what Apple's value is and 2) To future proof if Apple changes that value.
Ideally, I'd like something like that following:
@"H:|-(@750)-[myView]"
or @"H:|-(-@750)-[myView]"
But neither are accepted grammar. Is there a secret format string that would achieve this?
(I don't think there's a difference between iOS and MacOS for something like this, but if there is, I'm targetting MacOS.)
You cannot set priority for default spacing in visual format constraints. You can only do it when you create a constraint explicitly.
let constraint = NSLayoutConstraint(item: myView, attribute: .leadingMargin, relatedBy: .equal, toItem: superview, attribute: .leadingMargin, multiplier: 1, constant: 0)
constraint.priority = 750
You can get the default spacing by specifiying attributes as leadingMargin and trailingMargin instead of using just leading and trailing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With