我想以纵向模式在屏幕底部显示 UIView,因此当手机旋转并且水平方向将重新定位/调整所有子视图的大小时,一个 UIView 将保持原样,具有相同的大小和原始位置(即如果它在纵向模式的底部,则为水平方向的右端)。
有什么好的方法吗?
我想以纵向模式在屏幕底部显示 UIView,因此当手机旋转并且水平方向将重新定位/调整所有子视图的大小时,一个 UIView 将保持原样,具有相同的大小和原始位置(即如果它在纵向模式的底部,则为水平方向的右端)。
有什么好的方法吗?
您可以像这样设置自动调整大小的掩码:
myView.autorezisingmask = UIViewAutorezingMaskFlexibleTopMargin;
这将使视图保持在底部。
我可以想到几种方法来做到这一点。我在下面展示的一种方式完全依赖于使用约束。为此,这 3 个按钮不应该在它们自己的透明视图中,而只是要旋转的视图的子视图(在我的示例中是 self.view)。我不认为这 3 个按钮的原始约束很重要,因为我在旋转时将它们删除,但我开始使用的约束具有 centerX 约束的中心按钮,到底部的固定距离,到左侧的标准水平距离和右键,所有三个按钮的基线对齐。在 viewDidLoad 中,我循环遍历 self.view 的所有约束并确定与这些按钮有关的所有内容,并将它们放入一个数组中,以便我可以删除它们并稍后将它们添加回来。
@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;
}
}