简体   繁体   中英

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?

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 .

根据上面的注释,选择两个视图,然后在“ Pin”弹出窗口中,选中“相等高度”和“相等宽度”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM