Andrew Duncan
Andrew Duncan

Reputation: 3753

xcode constrain view to size of superview

Call it a fetish, but I'd like to do this in Interface Builder (aka part of Xcode now) instead of programmatically. I know that I can do this (inside a ViewController):

NSDictionary* dict = NSDictionaryOfVariableBindings(subview);
NSArray* constraints = 
    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[subview]|" // Hack or cool idea??
                                            options:0
                                            metrics:nil
                                              views:dict];
// And similar for V direction.
[self.view addConstraints:constraints];

But when I tell myself "this is just what IB could do for me" I am stymied. I get the "Align" and "Pin" popovers at lower-right, but most options are grey/inactive, and none seem to involve aligning or pinning to superview. Is there something wrong with my world-view of constraints, or am I just missing something?

Upvotes: 0

Views: 677

Answers (2)

Andrew Duncan
Andrew Duncan

Reputation: 3753

As per comment above, select both views, then in Pin popover, checkmark "Equal Heights" and "Equal Widths".

Upvotes: 1

c_rath
c_rath

Reputation: 3628

Once selecting a view, if you use the PIN option at the bottom right of the storyboard, the options should not be grayed out. If you notice they are still grayed out, deselect the view and select it once more. Occasionally I've selected a view and then clicked away from the screen and come back... the view still looked selected but I couldn't choose any options.

Keep in mind though that the PIN option at the bottom right is for the "spacing to closest neighbor". If you want to pin to the superview, you should select Editor->Pin->__ to Superview.

Upvotes: 1

Related Questions