Masonry横屏左右竖屏上下的两个视图

目的

本示例代码的目的有两个:

  1. 理解屏幕旋转的时候,视图控制器所调用的方法的作用。
  2. 理解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();
    }];

}

标号的注释解释

  1. 该方法在旋转发生的时候调用,方法内调用不同的布局方案。这里选用的是 - (void) viewDidLayoutSubviews 方法,而不是 - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator 方法。因为后者是will,是在旋转发生之前调用,感觉上并不直观。还有一种选择是使用通知。但是通知的相应方法会被多次(3次)调用的现象,所以这里也没有选择使用。
  2. label具有压缩阻力,会根据内容的多少来决定自身的尺寸。把它的压缩优先级设为1,用以保证它与父视图的约束关系。
  3. Masonry主要有三个方法来创建约束,分别为 mas_makeConstraints、mas_remakeConstraints、和mas_updateConstraints。第一个会直接创建约束,第二个会删除之前的约束再重新创建约束,第三个是不删除之前的约束添加新约束。

你可能感兴趣的:(Masonry横屏左右竖屏上下的两个视图)