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的位置和大小,不然可能不会显示

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,064评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,606评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,011评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,550评论 1 269
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,465评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,919评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,428评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,075评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,208评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,185评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,191评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,914评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,482评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,585评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,825评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,194评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,703评论 2 339

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,973评论 4 60
  • 如果你有什么治疗失眠和打发失眠夜的方法请速速联系我,在线等,挺急的。 我觉得失眠和我的心态和心理暗示有很大的...
    吃一大口可爱酱阅读 273评论 0 1
  • 20年来火影忍者给我们带来了快乐,就算如今鸣人死了,他也永远活在我们心中!向鸣人敬礼!
    张家之才阅读 218评论 0 1
  • 孤雁 独立荒原久, 茫然不复归。 早知两相失, 何若本孤飞? 题梦幻仙子镜湖入画照 仙子悠然身入画, 镜湖湛湛草盈...
    肖三羊阅读 1,042评论 9 4
  • 结果平等不可能即使可能也会造成社会效率低下,人的主观能动性下降,主观能动性下降必然导致社会协作低下。结果平等的初衷...
    MaleMonkey阅读 4,134评论 0 0