How to set priority in the default metric for automatic layout in visual format?

An auto market uses the - symbol to indicate that the restriction should use the default interval. Is there a way to set priority at this distance without explicitly specifying the metric value?

For example, the following visual format will create a leading constraint between myView and its superview, equal to the default value of Cocoa / CocoaTouch. But this limitation takes priority 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 give a leading constraint a priority, but not an explicit indicator. The reason is that 1) I do not want to guess what the meaning of Apple is, and 2) to future proof if Apple changes this value.

Ideally, I would like the following:

@"H:|-(@750)-[myView]" or @"H:|-( -@750 )-[myView]"

But not one of them is accepted in grammar. Is there a secret format string that would achieve this?

(I don't think there is a difference between iOS and MacOS for something similar, but if there is, I am targeting MacOS.)

+5
source share
1 answer

You cannot set the default spacing priority in visual format constraints. This can only be done if the restriction is explicitly set.

 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 specifying attributes as leadingMargin and trailingMargin instead of using only leading and trailing >.

0
source

Source: https://habr.com/ru/post/1204369/


All Articles