UIView忽略方向或坚持底部的容器

我想在纵向模式下在屏幕的底部显示UIView,所以当一个手机旋转和水平方向将重新定位/调整所有子视图,那一个UIView将保持原来的位置,具有相同的大小和原始位置(即在水平方向的右端,如果它在肖像模式的底部)。

有没有一个好的方法来做到这一点?

你可以像这样设置自动调整屏蔽:

myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin; 

这将保持观点在底部。

我可以想到几种方法来做到这一点。 一种方式,我下面显示仅依靠使用约束。 对于这个工作,3个button不应该在他们自己的透明视图中,而只是你想要旋转的视图的子视图(在我的例子中是self.view)。 我不认为这三个button的原始约束很重要,因为我在旋转时将它们移除了,但是我开始的约束条件是中心button具有centerX约束,到底部的固定距离,到左边的标准水平距离以及右键,所有三个button的基线alignment。 在viewDidLoad中,我遍历所有self.view的约束,并确定与这些button有关的所有内容,并将它们放入数组中,以便我可以删除它们并在稍后添加它们。

 @interface ViewController () @property (strong,nonatomic) NSMutableArray *portraitConstraints; @property (strong,nonatomic) NSMutableArray *landscapeConstraints; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.portraitConstraints = [NSMutableArray array]; self.landscapeConstraints = [NSMutableArray array]; for (NSLayoutConstraint *con in self.view.constraints) { if (con.firstItem == self.leftButton || con.secondItem == self.leftButton || con.firstItem == self.centerButton || con.secondItem == self.centerButton || con.firstItem == self.rightButton || con.secondItem == self.rightButton) { [self.portraitConstraints addObject:con]; } } } - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration { switch (interfaceOrientation) { case UIInterfaceOrientationLandscapeRight:{ [self.view removeConstraints:self.portraitConstraints]; [self.view removeConstraints:self.landscapeConstraints]; [self.landscapeConstraints removeAllObjects]; NSLayoutConstraint *centerYCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; NSLayoutConstraint *rightCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeRight relatedBy:0 toItem:self.view attribute:NSLayoutAttributeRight multiplier:1 constant:-8]; NSArray *stackCons= [NSLayoutConstraint constraintsWithVisualFormat:@"V:[left]-[center]-[right]" options:NSLayoutFormatAlignAllLeading metrics:nil views:@{@"left":self.leftButton, @"center":self.centerButton, @"right":self.rightButton}]; [self.landscapeConstraints addObject:centerYCon]; [self.landscapeConstraints addObject:rightCon]; [self.landscapeConstraints addObjectsFromArray:stackCons]; [self.view addConstraints:self.landscapeConstraints]; break; } case UIInterfaceOrientationLandscapeLeft:{ [self.view removeConstraints:self.portraitConstraints]; [self.view removeConstraints:self.landscapeConstraints]; [self.landscapeConstraints removeAllObjects]; NSLayoutConstraint *centerYCon2 = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]; NSLayoutConstraint *leftCon = [NSLayoutConstraint constraintWithItem:self.centerButton attribute:NSLayoutAttributeLeft relatedBy:0 toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:8]; NSArray *stackCons2= [NSLayoutConstraint constraintsWithVisualFormat:@"V:[left]-[center]-[right]" options:NSLayoutFormatAlignAllLeading metrics:nil views:@{@"left":self.leftButton, @"center":self.centerButton, @"right":self.rightButton}]; [self.landscapeConstraints addObject:centerYCon2]; [self.landscapeConstraints addObject:leftCon]; [self.landscapeConstraints addObjectsFromArray:stackCons2]; [self.view addConstraints:self.landscapeConstraints]; break; } case UIInterfaceOrientationPortrait:{ [self.view removeConstraints:self.landscapeConstraints]; [self.view addConstraints:self.portraitConstraints]; break; } default: break; } }