Fogmeister
Fogmeister

Reputation: 77661

Relative Auto Layout Constraints in Interface Builder

In code I can set constraints between two objects so that they are relative...

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:item1
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:item2
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:0.9
                                                       constant:0]];

Or so they have a constant difference...

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:item1
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:item2
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:1.0
                                                       constant:-30]];

Or event so they relate different attributes of the same item...

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:item1
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:item1
                                                      attribute:NSLayoutAttributeHeight
                                                     multiplier:1.0
                                                       constant:0]];

Is it possible to apply these type of constraints using Interface Builder?

Upvotes: 3

Views: 4294

Answers (3)

scottdev
scottdev

Reputation: 141

Note that you can enter in percentage as well such as 50% The side arrows allow incrementing and decrementing by .1 or by 1%

Setting a % multiplier (constant 0) relative of the superview (or image view) center x,y is handy to allow a label or textfield to track a specific place in a view or image regardless of it's final position or scale.

You can see what the constraints do by selecting main view and going to: Editor -> Resolve auto layout issues -> Update Frames and the different elements will move based on their constraints.

Sometimes IB updates on the fly but usually not.

Upvotes: 0

Albert Tong
Albert Tong

Reputation: 411

It's possible in interface builder. Select the IB item in question and inspect the constraints on it. Select and edit will reveal the multiplier settings of the constraint.

AutolayoutRelativeMultiplierInterfaceBuilder

Upvotes: 3

Eric Castro
Eric Castro

Reputation: 134

It is possible now in XCode 5.1.

This is a change from 5.0. When you add for example an "Equal Widths" constraint you can now go to the inspector, there's a new field where you can input the multiplier value (among other new things), thus creating a proportionally sized view which can be related to its container or some other view within the hierarchy.

Upvotes: 3

Related Questions