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<UIViewControllerTransitionCoordinator>)coordinator 方法。因为后者是will,是在旋转发生之前调用,感觉上并不直观。还有一种选择是使用通知。但是通知的相应方法会被多次(3次)调用的现象,所以这里也没有选择使用。
  2. label具有压缩阻力,会根据内容的多少来决定自身的尺寸。把它的压缩优先级设为1,用以保证它与父视图的约束关系。
  3. Masonry主要有三个方法来创建约束,分别为 mas_makeConstraints、mas_remakeConstraints、和mas_updateConstraints。第一个会直接创建约束,第二个会删除之前的约束再重新创建约束,第三个是不删除之前的约束添加新约束。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使...
    丘比沙拉阅读 3,190评论 2 19
  • Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使...
    爱敲代码的果果阅读 1,919评论 0 2
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • 今天是21天写作打卡的最后一天,坚持21天输出并没有想象中的艰难,我知是因为偶尔偷懒、拈来旧文胡乱修改凑数了的。坚...
    Simmel阅读 267评论 0 0
  • 以前的自己突然被否定了。曾经以为自己是一个虽然不算积极向上但也至少青春阳光的人,也许曾经是,后来发生了很多事,把我...
    南瓜头二号阅读 190评论 0 1