我想在縱向模式下在屏幕底部顯示UIView,所以當手機旋轉和水平方向將重新定位/調整所有子視圖時,該UIView將保留它在哪裏,具有相同的尺寸和原始位置(即,如果它位於縱向模式的底部,則位於水平方向的右端)。UIView忽略方向或底部到底容器
有沒有很好的方法來做到這一點?
我想在縱向模式下在屏幕底部顯示UIView,所以當手機旋轉和水平方向將重新定位/調整所有子視圖時,該UIView將保留它在哪裏,具有相同的尺寸和原始位置(即,如果它位於縱向模式的底部,則位於水平方向的右端)。UIView忽略方向或底部到底容器
有沒有很好的方法來做到這一點?
我可以想到幾種方法來做到這一點。一種方式,我在下面展示的僅僅依賴於使用約束。爲了這個工作,3個按鈕不應該在他們自己的透明視圖中,而只是你想要旋轉的視圖的子視圖(在我的例子中是self.view)。我並不認爲這三個按鈕的原始約束很重要,因爲我在旋轉時移除了它們,但是我開始的約束條件是中心按鈕具有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;
}
}
我不喜歡自動佈局,但看起來像改變方向的句柄是唯一的方法。 此外這個問題必須有幫助:http://stackoverflow.com/questions/6104963/placing-and-moving-views-on-rotation-uiview?rq=1 – kaspartus
我認爲做到這一點的唯一方法是不允許此控制器旋轉,然後將變換應用到要旋轉的屏幕部分。 – rdelmar
你想要保留這個視圖的任何子視圖也不旋轉嗎?那麼,從這個角度來看,一切都會站在它的一邊? – rdelmar