我试图创建上2次视图控制器,它看起来就像在IB 如何使具有2次覆盖屏幕,而不是重叠
屏幕但他们只在iPhone 5s上以这种方式行事 在其他情况下,它看起来像在横向上的图像上显示的方式。我使用大小类来为不同的屏幕方向设置约束。
我试图创建上2次视图控制器,它看起来就像在IB 如何使具有2次覆盖屏幕,而不是重叠
屏幕但他们只在iPhone 5s上以这种方式行事 在其他情况下,它看起来像在横向上的图像上显示的方式。我使用大小类来为不同的屏幕方向设置约束。
所以这个问题似乎是早于iPhone 6+的iPhone在横向上具有wCompact hCompact大小类。你应该记住不要使用像wCompact hAny这样的大小类,因为它们会覆盖该紧凑大小类。
所以,实际上,你在这种情况下需要的是创建3个大小类: wCompact hRegular - 所有人像 wCompact hCompact - iPhone手机比6+ wRegular hCompact更早 - 所有其他
任何/紧凑意味着紧凑型宽任何电话,其中iPhone的小于6+的。您需要为Regular/Compact设置约束条件,以便在Portrait中为6+以外的iPhone设置约束条件。
这种事情是比较容易与UIStackView 多。
有一个good tutorial here;我强烈建议你在做一些艰难的工作之前先设置一些堆栈视图。
添加到您的的viewController
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的viewDidLoad中方法:的firstView属性:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view属性:NSLayoutAttributeTop 乘数:1.0F 常数: 0.0f
];
NSLayoutConstraint * centerFirstViewHeightConstraint = [ NSLayoutConstraint constraintWithItem:的firstView 属性:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:自我。查看 属性: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]];
我这样做,但它并没有帮助(( – AOY
)如果你删除任何大小的任何类型的宽度或任何高度的限制?只是正确定义的? – choli