Cocoa Auto Layout Visual Format: Align views without setting space constraints

620 Views Asked by At

I try to implement the following constraints with Auto Layout Visual Format:

  1. Standard distance from container view to label
  2. Fixed distance from container view to textField
  3. Baseline alignment between label and textField

The first two constraints are straightforward:

var viewDict: [NSObject:AnyObject] = [:]
viewDict["label"] = label
viewDict["textField"] = textField

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[label]", options: nil, metrics: nil, views: viewDict))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-100-[textField]", options: nil, metrics: nil, views: viewDict))

To align two views I understand I have to set the options parameter accordingly. According to the spec, the options parameter applies to the views stated in the format string parameter.

But how do I list views in the format string without making assumptions regarding the space between the views?

If I do something like this I create an unwanted constraint regarding the distance between label and textField:

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("[label]-[inputField]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewDict))

I guess I could work around it by setting the priority of the unnecessary size constraint to zero, but still I have the feeling I'm missing something conceptually here.

Is it advisable not to use Visual Format in such a scenario?

0

There are 0 best solutions below