【发布时间】:2016-05-03 12:22:17
【问题描述】:
我正在尝试创建一个带有 2 个视图的视图控制器,看起来就像在 IB 中一样
但它们只有在 iPhone 5s 上才有这种行为 在其他情况下,它看起来像横向图像上显示的方式。我使用尺寸类来设置不同屏幕方向的约束。
【问题讨论】:
标签: ios interface-builder nslayoutconstraint size-classes
我正在尝试创建一个带有 2 个视图的视图控制器,看起来就像在 IB 中一样
但它们只有在 iPhone 5s 上才有这种行为 在其他情况下,它看起来像横向图像上显示的方式。我使用尺寸类来设置不同屏幕方向的约束。
【问题讨论】:
标签: ios interface-builder nslayoutconstraint size-classes
Any/Compact 是指任何具有 Compact 宽度的手机,小于 6+ 的 iPhone 就是这样。您需要设置 Regular/Compact 的约束,才能在 Portrait 中为 6+ 以外的 iPhone 设置约束。
【讨论】:
使用 UIStackView,这种事情要容易得多。
有一个good tutorial here;我强烈建议您先练习一下使用堆栈视图进行设置,然后再以艰难的方式进行操作。
【讨论】:
你应该给出类似的约束,
top, bottom, leading and trailing 到两个视图。
然后选择两个视图并给equal height约束,你会得到想要的结果。
您可以在any any 中提供。它适用于每个方向。
希望这会有所帮助:)
【讨论】:
将此添加到您的 viewController 的 viewDidLoad 方法中
UIView *firstView = [[UIView alloc] init];
self.navigationController.navigationBar.translucent = false;
firstView.backgroundColor = [UIColor redColor];
firstView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:firstView];
NSLayoutConstraint *topFirstViewYConstraint = [
NSLayoutConstraint constraintWithItem:firstView 属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeTop 乘数:1.0f 常数:0.0f
];
NSLayoutConstraint *centerFirstViewHeightConstraint = [ NSLayoutConstraint constraintWithItem:firstView 属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeHeight 乘数:0.5f 常数:0.0 ];
NSLayoutConstraint *centerFirstViewWidthConstraint = [ NSLayoutConstraint constraintWithItem:firstView 属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeWidth 乘数:1.0f 常量:0.0f ]; [super.view addConstraints:@[ topFirstViewYConstraint, centerFirstViewHeightConstraint,centerFirstViewWidthConstraint]];
UIView *secondView = [[UIView alloc] init];
secondView.backgroundColor = [UIColor greenColor];
secondView.translatesAutoresizingMaskIntoConstraints = false;
[self.view addSubview:secondView];
NSLayoutConstraint *topSecondViewYConstraint = [
NSLayoutConstraint constraintWithItem:secondView 属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:firstView 属性:NSLayoutAttributeBottom 乘数:1.0f 常数:0.0f ]; NSLayoutConstraint *centerSecondViewHeightConstraint = [ NSLayoutConstraint constraintWithItem:secondView 属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeHeight 乘数:0.5f 常数:0.0 ]; NSLayoutConstraint *centerSecondViewWidthConstraint = [ NSLayoutConstraint constraintWithItem:secondView 属性:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view 属性:NSLayoutAttributeWidth 乘数:1.0f 常数:0.0f ]; [super.view addConstraints:@[ topSecondViewYConstraint, centerSecondViewHeightConstraint, centerSecondViewWidthConstraint]];
【讨论】:
所以问题似乎是 iPhone 6+ 之前的 iPhone 具有横向的 wCompact hCompact 尺寸类。你应该记住不要使用像 wCompact hAny 这样的大小类,因为它们会覆盖那个紧凑的大小类。
所以,实际上,在这种情况下,您需要创建 3 个尺寸等级: wCompact hRegular - 所有肖像 wCompact hCompact - 6+ 之前的 iPhone wRegular hCompact - 所有其他
【讨论】: