iOS 純代碼玩轉(zhuǎn)自動(dòng)布局

無(wú)論是移動(dòng)端還是PC端開(kāi)發(fā),炫酷的UI直接提高了應(yīng)用的檔次,下面來(lái)說(shuō)說(shuō)我iOS開(kāi)發(fā)中,是如何優(yōu)雅的布局的

一、Masonry

先上一段代碼感受一下,這是設(shè)置視頻播放器下面的工具條的布局

- (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);
    }];
    
    // 開(kāi)始按鈕
    [_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);
    }];
    
    // 進(jìn)度條
    [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);
    }];
    
    // 時(shí)間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的設(shè)置都在block里面,自動(dòng)布局又方便維護(hù),<code>MASConstraintMaker</code>這個(gè)是關(guān)鍵類(lèi),有以下屬性方法

@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;// 左對(duì)齊
@property (nonatomic, strong, readonly) MASConstraint *trailing;// 右對(duì)齊
@property (nonatomic, strong, readonly) MASConstraint *width;// 寬
@property (nonatomic, strong, readonly) MASConstraint *height;// 高
@property (nonatomic, strong, readonly) MASConstraint *centerX;// X居中對(duì)齊
@property (nonatomic, strong, readonly) MASConstraint *centerY;// Y居中對(duì)齊
@property (nonatomic, strong, readonly) MASConstraint *baseline;// 基線

獲取相應(yīng)的屬性值是這樣的

@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.設(shè)置間距后用offset添加偏移量

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

2.設(shè)置寬的比例

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

3.設(shè)置參照的視圖等寬高

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

4.設(shè)置內(nèi)邊距

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

二、SDAutoLayout

這個(gè)庫(kù)也比較好用,鏈?zhǔn)骄幊田L(fēng)格,還能自適應(yīng)tableViewCell的高度,較輕量級(jí),有興趣可以學(xué)習(xí)學(xué)習(xí),先上一段代碼

     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);

// 自適應(yīng)最后一個(gè)控件的高度
    [scrollView setupAutoHeightWithBottomView:expressionItemView bottomMargin:20];

屬性一看就知道,大家都是成年人,不用多說(shuō)了吧

需要注意點(diǎn):
1.要先加到父視圖上,再設(shè)置布局
2.可以一個(gè)個(gè)的add,也可以調(diào)sd_addSubviews一次性add
3.布局要能確定View的位置和大小,不然可能不會(huì)顯示

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,419評(píng)論 4 61
  • 如果你有什么治療失眠和打發(fā)失眠夜的方法請(qǐng)速速聯(lián)系我,在線等,挺急的。 我覺(jué)得失眠和我的心態(tài)和心理暗示有很大的...
    吃一大口可愛(ài)醬閱讀 359評(píng)論 0 1
  • 伊斯坦布爾是一個(gè)同時(shí)跨越歐、亞兩大洲的名城,位于土耳其西北部馬爾馬拉地區(qū),而位于黑海和馬爾馬拉海之間的博斯普魯...
    流浪的毛毛閱讀 543評(píng)論 9 13
  • 20年來(lái)火影忍者給我們帶來(lái)了快樂(lè),就算如今鳴人死了,他也永遠(yuǎn)活在我們心中!向鳴人敬禮!
    張家之才閱讀 277評(píng)論 0 1
  • 孤雁 獨(dú)立荒原久, 茫然不復(fù)歸。 早知兩相失, 何若本孤飛? 題夢(mèng)幻仙子鏡湖入畫(huà)照 仙子悠然身入畫(huà), 鏡湖湛湛草盈...
    肖三羊閱讀 1,180評(píng)論 9 4

友情鏈接更多精彩內(nèi)容