Ios8 使用autolayout为viewcontroller实现此布局
我正在开发一个viewcontroller,我想尝试实现下面的图片。我想这样做,使它看起来伟大的任何设备方面的纵横比 顶部是容器,中间是collectionview,底部是uitableview。 我想保留的是纵横比。我的想法如下:Ios8 使用autolayout为viewcontroller实现此布局,ios8,autolayout,xcode6,Ios8,Autolayout,Xcode6,我正在开发一个viewcontroller,我想尝试实现下面的图片。我想这样做,使它看起来伟大的任何设备方面的纵横比 顶部是容器,中间是collectionview,底部是uitableview。 我想保留的是纵横比。我的想法如下: 对于第一个框,将前导边距、尾随边距和顶边距设置为容器(准则)。将底部的框设置为下面的框(较大的中间框)。同时设置纵横比 对于中间框,将前导/尾随页边距设置为参考线,并将底部设置为下面的框。还可以设置纵横比 对于最后一个框,设置前导、尾随、底部(相对于基准线)以及纵
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7f8a66031bc0 V:[UITableView:0x7f8a65837c00(73)]>",
"<NSLayoutConstraint:0x7f8a6605c150 UITableView:0x7f8a65837c00.width == 7.78082*UITableView:0x7f8a65837c00.height>",
"<NSLayoutConstraint:0x7f8a6604e970 UICollectionView:0x7f8a65838400.leading == UIView:0x7f8a66031eb0.leadingMargin>",
"<NSLayoutConstraint:0x7f8a6604e9c0 UICollectionView:0x7f8a65838400.trailing == UIView:0x7f8a66031eb0.trailingMargin>",
"<NSLayoutConstraint:0x7f8a6604ea10 UICollectionView:0x7f8a65838400.width == UITableView:0x7f8a65837c00.width>",
"<NSLayoutConstraint:0x7f8a63c4ccf0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f8a66031eb0(320)]>"
)
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
"",
"",
""
)
非常感谢 比如说,您希望俯视图的高度为主视图的20%,中间视图的高度为主视图的50%。您可以通过以下方式编程完成此操作:
[topView setTranslatesAutoresizingMaskIntoConstraints: NO];
[middleView setTranslatesAutoresizingMaskIntoConstraints: NO];
[bottomView setTranslatesAutoresizingMaskIntoConstraints: NO];
NSDictionary *views = @{@"topView": topView, @"middleView": middleView, @"bottomView": bottomView};
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[topView]|" options: 0 metrics: nil views: views]];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[middleView]|" options: 0 metrics: nil views: views]];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[bottomView]|" options: 0 metrics: nil views: views]];
[self.view addConstraints: [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[topView][middleView][bottomView]|" options: 0 metrics: nil views: views]];
[self.view addConstraint: [NSLayoutConstraint constraintWithItem: topView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeHeight multiplier: 0.2f constant: 0.0f]];
[self.view addConstraint: [NSLayoutConstraint constraintWithItem: middleView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationEqual toItem: self.view attribute: NSLayoutAttributeHeight multiplier: 0.5f constant: 0.0f]];
您不需要为底部视图设置纵横比高度。只需将底部视图与主视图的底部边缘固定在一起
如果要在Interface Builder中执行此操作,可以通过以下方式执行:
当设备本身具有不同的纵横比时,无法保留所有W视图的纵横比。可以为每个视图设置高度的比例。或者您可以设置其中两个视图的纵横比,让第三个视图更改其纵横比以使用剩余的空间。