如何制作一个屏幕,其中有2个视图覆盖屏幕而不重叠

时间:2016-05-03 12:22:18

标签: ios interface-builder nslayoutconstraint size-classes

我正在尝试创建一个包含2个视图的视图控制器,它看起来就像IB enter image description here

enter image description here

但他们只在iPhone 5s上表现出来 在其他情况下,它看起来在横向图像上显示的方式。我使用大小类来设置不同屏幕方向的约束。

enter image description here

这就是iPhone 6s加上enter image description here

的样子

5 个答案:

答案 0 :(得分:1)

Any / Compact意味着任何手机的宽度都是紧凑的,iPhone的尺寸小于6+。您需要为Regular / Compact设置约束,以便为Portrait中的6+以外的iPhone设置约束。

答案 1 :(得分:1)

使用UIStackView,这种事情更容易

  1. 选择您的两个观点。
  2. 点击右下方的图标,在它们周围创建一个UIStackView。
  3. 按方向将新UIStackView设置为水平或垂直。
  4. 将其固定到封闭视图。而且你已经完成了!
  5. 有一个good tutorial here;我强烈建议你在做好事情之前先用堆栈视图进行练习。

答案 2 :(得分:0)

你应该给出约束,

top, bottom, leading and trailing同时查看。

然后选择两个视图并给出equal height约束,您将获得所需的结果。

你可以在any any中提供。它适用于每个方向。

希望这会有所帮助:)

答案 3 :(得分:0)

将此添加到您的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 attribute: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                                                    attribute: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]];

答案 4 :(得分:0)

所以问题似乎是iPhone 6+之前的iPhone具有针对格局的wCompact hCompact尺寸等级。你应该记住不要使用像wCompact hAny这样的大小类,因为它们会覆盖那个紧凑的大小类。

所以,实际上,在这种情况下你需要创建3个大小的类: wCompact hRegular - 所有肖像 wCompact hCompact - 早于6+的iPhone wRegular hCompact - 所有其他