Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set a priority on auto-layouts default spacing metric in a visual format?

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.)

like image 421
kennyc Avatar asked Oct 09 '14 21:10

kennyc


1 Answers

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.

like image 165
kruttinangopal Avatar answered Oct 21 '22 06:10

kruttinangopal