iOS 纯代码玩转自动布局

无论是移动端还是PC端开发,炫酷的UI直接提高了应用的档次,下面来说说我iOS开发中,是如何优雅的布局的

一、Masonry

先上一段代码感受一下,这是设置视频播放器下面的工具条的布局

- (void)updateBottomToolbarView
{
    // 底部工具条
    [_bottomToolbar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self).with.offset(0);
    }];
    
    // 开始按钮
    [_playOrPauseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomToolbar).with.offset(0);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.bottomToolbar).with.offset(0);
        make.width.mas_equalTo(40);
    }];
    
    // 进度条
    [self.progressSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomToolbar).with.offset(45);
        make.right.equalTo(self.bottomToolbar).with.offset(-45);
        make.height.mas_equalTo(40);
        make.top.equalTo(self.bottomToolbar).with.offset(0);
    }];
    
    // 时间Label
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomToolbar).with.offset(45);
        make.right.equalTo(self.bottomToolbar).with.offset(-45);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(self.bottomToolbar).with.offset(0);
    }];

    // 全屏按钮
    [_fullScreenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bottomToolbar).with.offset(0);
        make.height.mas_equalTo(40);
        make.bottom.equalTo(self.bottomToolbar).with.offset(0);
        make.width.mas_equalTo(40);
    }];
}

所有的Frame的设置都在block里面,自动布局又方便维护,<code>MASConstraintMaker</code>这个是关键类,有以下属性方法

@property (nonatomic, strong, readonly) MASConstraint *left;// 左间距
@property (nonatomic, strong, readonly) MASConstraint *top; // 上间距
@property (nonatomic, strong, readonly) MASConstraint *right;// 右间距
@property (nonatomic, strong, readonly) MASConstraint *bottom;// 下间距
@property (nonatomic, strong, readonly) MASConstraint *leading;// 左对齐
@property (nonatomic, strong, readonly) MASConstraint *trailing;// 右对齐
@property (nonatomic, strong, readonly) MASConstraint *width;// 宽
@property (nonatomic, strong, readonly) MASConstraint *height;// 高
@property (nonatomic, strong, readonly) MASConstraint *centerX;// X居中对齐
@property (nonatomic, strong, readonly) MASConstraint *centerY;// Y居中对齐
@property (nonatomic, strong, readonly) MASConstraint *baseline;// 基线

获取相应的属性值是这样的

@property (nonatomic, strong, readonly) MASViewAttribute *mas_left;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_top;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_right;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottom;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_leading;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_trailing;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_width;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_height;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerX;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_centerY;
@property (nonatomic, strong, readonly) MASViewAttribute *mas_baseline;
@property (nonatomic, strong, readonly) MASViewAttribute *(^mas_attribute)(NSLayoutAttribute attr);

我们可以这样玩
1.设置间距后用offset添加偏移量

make.left.equalTo(self.view.mas_left).offset(0);

2.设置宽的比例

make.width.equalTo(self.view.mas_width).multipliedBy(0.5)

3.设置参照的视图等宽高

make.width.and.height.equalTo(self.imageView);

4.设置内边距

make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

二、SDAutoLayout

这个库也比较好用,链式编程风格,还能自适应tableViewCell的高度,较轻量级,有兴趣可以学习学习,先上一段代码

     expressionImage.sd_layout
    .topSpaceToView(scrollView,0)
    .leftSpaceToView(scrollView,0)
    .rightSpaceToView(scrollView,0)
    .heightIs(150);
    
    expressionName.sd_layout
    .topSpaceToView(expressionImage,10)
    .leftSpaceToView(scrollView, 15)
    .heightIs(30)
    .widthIs(80);
    
    expressionDownLoad.layer.cornerRadius = 3.0;
    expressionDownLoad.sd_layout
    .topEqualToView(expressionName)
    .rightSpaceToView(scrollView,20)
    .widthIs(80)
    .heightIs(30);
    
    expressionDetail.sd_layout
    .leftEqualToView(expressionName)
    .rightSpaceToView(scrollView,20)
    .topSpaceToView(expressionName,10)
    .autoHeightRatio(0);

// 自适应最后一个控件的高度
    [scrollView setupAutoHeightWithBottomView:expressionItemView bottomMargin:20];

属性一看就知道,大家都是成年人,不用多说了吧

需要注意点:
1.要先加到父视图上,再设置布局
2.可以一个个的add,也可以调sd_addSubviews一次性add
3.布局要能确定View的位置和大小,不然可能不会显示

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,191评论 4 61
  • 如果你有什么治疗失眠和打发失眠夜的方法请速速联系我,在线等,挺急的。 我觉得失眠和我的心态和心理暗示有很大的...
    吃一大口可爱酱阅读 2,286评论 0 1
  • 伊斯坦布尔是一个同时跨越欧、亚两大洲的名城,位于土耳其西北部马尔马拉地区,而位于黑海和马尔马拉海之间的博斯普鲁...
    流浪的毛毛阅读 3,395评论 9 13
  • 20年来火影忍者给我们带来了快乐,就算如今鸣人死了,他也永远活在我们心中!向鸣人敬礼!
    张家之才阅读 1,599评论 0 1
  • 孤雁 独立荒原久, 茫然不复归。 早知两相失, 何若本孤飞? 题梦幻仙子镜湖入画照 仙子悠然身入画, 镜湖湛湛草盈...
    肖三羊阅读 4,738评论 9 4