简体   繁体   English

xcode将视图限制为超级视图的大小

[英]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. 称其为恋物癖,但我想在Interface Builder(现在又称为Xcode的一部分)中进行此操作,而不是通过编程方式进行。 I know that I can do this (inside a ViewController): 我知道我可以做到这一点(在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. 但是,当我告诉自己“这就是IB可以为我做的事情”时,我受阻了。 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. 选择视图后,如果使用情节提要板右下角的PIN选项,则这些选项不应显示为灰色。 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". 请记住,尽管右下角的PIN选项是“间隔到最近的邻居”。 If you want to pin to the superview, you should select Editor->Pin->__ to Superview . 如果要固定到超级视图,则应选择Editor-> Pin-> __ to Superview

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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