Dan*_*anM 6 interface-builder xib ios autolayout
我有一个使用自动布局构建的故事板.在那个故事板中,我在几个位置嵌入了一个UIViewController子类(ButtonGridViewController),每个位置都有不同的大小. ButtonGridViewController的视图是用xib定义的.
我需要的是整个ButtonGridViewController视图只是缩放以填充我嵌入它的视图.使用旧的struts-and-spring方法,这是微不足道的 - 只需将所有子视图设置为调整大小在两个方向,瞧,小菜一碟.
如何使用约束完成相同的操作?对于它的价值,xib只包含一个主视图,它是矩形的,有4个子视图 - 每个按钮 - 排列成2x2网格.我想要所有东西,包括按钮和间距,来缩放和/或拉伸以填充它进入的视图.
谢谢!
rai*_*ade 11
要使用约束完成相同的操作,您需要将superview的前导,尾随,顶部和底部空间设置为0.请参阅下文:
//load the ButtonGridViewController from a xib
[[NSBundle mainBundle] loadNibNamed:@"..." owner:self options:nil];
//get the view add it
[self.view addSubView:self.myGridView];
//turn off springs and struts
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
//add constraints to fill parent view
NSArray *arr;
//horizontal constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"|[vw]|"
options:0
metrics:nil
views:@{@"vw":self.myGridView}];
[self.view addConstraints:arr];
//vertical constraints
arr = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[vw]|"
options:0
metrics:nil
views:@{@"vw":self.myGridView}];
[self.view addConstraints:arr];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14022 次 |
| 最近记录: |