目的
本示例代碼的目的有兩個:
- 理解屏幕旋轉的時候,視圖控制器所調用的方法的作用。
- 理解Masonry的基本使用。
實現后的效果 (不支持upside down方向)
screenRotating.gif
示例代碼
// 1
// 屏幕旋轉時調用
- (void) viewDidLayoutSubviews {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
switch (orientation) {
case UIInterfaceOrientationPortrait:
NSLog(@"portrait");
[self verticalTwoViews];
break;
case UIInterfaceOrientationLandscapeLeft:
NSLog(@"landscape left");
[self horizontalTwoViews];
break;
case UIInterfaceOrientationLandscapeRight:
NSLog(@"landscape right");
[self horizontalTwoViews];
break;
case UIInterfaceOrientationPortraitUpsideDown:
NSLog(@"portrait upside down");
break;
case UIInterfaceOrientationUnknown:
break;
default:
break;
}
}
// 兩個視圖垂直排列
- (void) verticalTwoViews {
if (self.leftView == nil) {
self.leftView = [[UIView alloc] initWithFrame:CGRectZero];
self.leftView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.leftView];
self.leftLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.leftView addSubview:self.leftLabel];
self.leftLabel.numberOfLines = 0;
self.leftLabel.backgroundColor = [UIColor whiteColor];
self.leftLabel.text = @"揭諦揭諦波羅揭諦波羅僧揭諦菩提娑婆訶";
// 2
[self.leftLabel setContentCompressionResistancePriority:1 forAxis:UILayoutConstraintAxisHorizontal];
[self.leftLabel mas_makeConstraints:^(MASConstraintMaker * make) {
make.top.left.right.equalTo(self.leftView).mas_offset(UIEdgeInsetsMake(20, 20, 0, 20)).priorityMedium();
}];
}
if (self.rightView == nil) {
self.rightView = [[UIView alloc] initWithFrame:CGRectZero];
self.rightView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.rightView];
self.rightLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[self.rightView addSubview:self.rightLabel];
self.rightLabel.numberOfLines = 0;
self.rightLabel.backgroundColor = [UIColor whiteColor];
self.rightLabel.text = @"揭諦揭諦波羅揭諦波羅僧揭諦菩提娑婆訶";
[self.rightLabel setContentCompressionResistancePriority:1 forAxis:UILayoutConstraintAxisHorizontal];
[self.rightLabel mas_makeConstraints:^(MASConstraintMaker * make) {
make.top.left.right.equalTo(self.rightView).mas_offset(UIEdgeInsetsMake(20, 20, 0, 20)).priorityMedium();
}];
}
// 3
[self.leftView mas_remakeConstraints:^(MASConstraintMaker * make) {
make.top.left.right.equalTo(self.view).mas_offset(UIEdgeInsetsMake(30, 30, 0, 30)).priorityMedium();
}];
[self.rightView mas_remakeConstraints:^(MASConstraintMaker * make) {
make.left.bottom.right.equalTo(self.view).mas_offset(UIEdgeInsetsMake(0, 30, 30, 30)).priorityMedium();
make.top.equalTo(self.leftView.mas_bottom).offset(30).priorityMedium();
make.height.equalTo(self.leftView.mas_height).priorityLow();
}];
}
// 兩個視圖水平排列
- (void) horizontalTwoViews {
if (self.leftView == nil) {
self.leftView = [[UIView alloc] initWithFrame:CGRectZero];
self.leftView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.leftView];
}
if (self.rightView == nil) {
self.rightView = [[UIView alloc] initWithFrame:CGRectZero];
self.rightView.backgroundColor = [UIColor blueColor];
[self.view addSubview:self.rightView];
}
[self.leftView mas_remakeConstraints:^(MASConstraintMaker * make) {
make.top.left.bottom.equalTo(self.view).mas_offset(UIEdgeInsetsMake(30, 30, 30, 0)).priorityMedium();
}];
[self.rightView mas_remakeConstraints:^(MASConstraintMaker * make) {
make.top.bottom.right.equalTo(self.view).mas_offset(UIEdgeInsetsMake(30, 0, 30, 30)).priorityMedium();
make.left.equalTo(self.leftView.mas_right).mas_offset(@30).priorityMedium();
make.width.equalTo(self.leftView.mas_width).priorityLow();
}];
}
標號的注釋解釋
- 該方法在旋轉發生的時候調用,方法內調用不同的布局方案。這里選用的是 - (void) viewDidLayoutSubviews 方法,而不是 - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator 方法。因為后者是will,是在旋轉發生之前調用,感覺上并不直觀。還有一種選擇是使用通知。但是通知的相應方法會被多次(3次)調用的現象,所以這里也沒有選擇使用。
- label具有壓縮阻力,會根據內容的多少來決定自身的尺寸。把它的壓縮優先級設為1,用以保證它與父視圖的約束關系。
- Masonry主要有三個方法來創建約束,分別為 mas_makeConstraints、mas_remakeConstraints、和mas_updateConstraints。第一個會直接創建約束,第二個會刪除之前的約束再重新創建約束,第三個是不刪除之前的約束添加新約束。