iosswiftvisual-format-language

Swift: Visual Format Language: Fill label and fix button


I have the following layout constraint:

NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[label]-60-[button]-10-|", 
options: .AlignAllCenterY, 
metrics: nil, 
views: views)

but if [label] is too long, the [button] gets pushed off the screen.

Is it possible to 'fix' the [button] on the right, and have the [label] fill the remainder of the space on the left?

Thanks.


Solution

  • The reason this happens is that all your constraints cannot be simultaneously satisfied. In your case, you need to allow the system to "shrink" the label if necessary to fit all the content on the line.

    You can do this by reducing the compression resistance priority of the label:

    label.setContentCompressionResistancePriority(UILayoutPriorityDefaultLow, forAxis: .Horizontal)