最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS自適配利器Masonry使用指南

 更新時(shí)間:2016年01月20日 08:46:22   作者:FlyElephant  
如果說(shuō)自動(dòng)布局解救了多屏幕適配,那眾多三方庫(kù)的出現(xiàn)就解救了系統(tǒng)自動(dòng)布局的寫(xiě)法。Masonry就是其中一個(gè)。用法上也比較簡(jiǎn)單靈活,很大程度上替代了傳統(tǒng)的NSLayoutConstraint布局方式。下面我們就來(lái)具體探討下吧

關(guān)于iOS布局自動(dòng)iPhone6之后就是AutoLayOut,AutoLayOut固然非常好用,不過(guò)有時(shí)候我們需要在頁(yè)面手動(dòng)進(jìn)行頁(yè)面布局,VFL算是一種選擇,而且VFL不復(fù)雜,理解起來(lái)很容易,實(shí)際開(kāi)發(fā)中用的特別熟還好,要是第一次看估計(jì)要花點(diǎn)功夫才能搞定。Masonry算是VFL的簡(jiǎn)化版,用的人比較多,之前項(xiàng)目中用過(guò)一次,對(duì)手動(dòng)寫(xiě)頁(yè)面的開(kāi)發(fā)來(lái)說(shuō)算是福利。

基礎(chǔ)知識(shí)

首先我們看一個(gè)常見(jiàn)的問(wèn)題將一個(gè)子View放在的UIViewController的某個(gè)位置,通過(guò)設(shè)置邊距來(lái)實(shí)現(xiàn),效果如下:

如果通過(guò)VFL我們代碼會(huì)是這樣的:

UIView *superview                               = self.view;
 
UIView *view1                                   = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor                           = [UIColor redColor];
[superview addSubview:view1];
 
UIEdgeInsets padding                            = UIEdgeInsetsMake(200, 50, 200, 50);
 
[superview addConstraints:@[
 
                            //約束
                            [NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeTop
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeTop
                                                        multiplier:1.0
                                                          constant:padding.top],
 
                            [NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeLeft
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeLeft
                                                        multiplier:1.0
                                                          constant:padding.left],
 
                            [NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeBottom
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeBottom
                                                        multiplier:1.0
                                                          constant:-padding.bottom],
 
                            [NSLayoutConstraint constraintWithItem:view1
                                                         attribute:NSLayoutAttributeRight
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:superview
                                                         attribute:NSLayoutAttributeRight
                                                        multiplier:1
                                                          constant:-padding.right],
 
                            ]];

只是簡(jiǎn)單的設(shè)置了一個(gè)邊距,如果視圖的關(guān)系比較復(fù)雜,維護(hù)起來(lái)會(huì)是一個(gè)很痛苦的事情,我們看一下Masonry是如何實(shí)現(xiàn)的,導(dǎo)入Masonry.h頭文件,約束的代碼:

UIView  *childView=[UIView new];
[childView setBackgroundColor:[UIColor redColor]];
//先將子View加入在父視圖中
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
UIEdgeInsets padding = UIEdgeInsetsMake(200, 50, 200, 50);
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(weakSelf.view).with.insets(padding);
}];

通過(guò)mas_makeConstraints設(shè)置邊距有種鳥(niǎo)槍換炮的感覺(jué),我們即將開(kāi)啟一段新的旅程,可以緊接著看下面比較實(shí)用的功能~

實(shí)用知識(shí)

1.View設(shè)置大小

UIView  *childView=[UIView new];
[childView setBackgroundColor:[UIColor redColor]];
//先將子View加入在父視圖中
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
    //設(shè)置大小
    make.size.mas_equalTo(CGSizeMake(100, 100));
    //居中
    make.center.equalTo(weakSelf.view);
}];

效果如下:

  

這里友情其實(shí)一個(gè)小內(nèi)容,目前我們?cè)O(shè)置約束都是通過(guò)mas_makeConstraints用來(lái)創(chuàng)建約束,mas_updateConstraints用來(lái)更新約束,mas_remakeConstraints重置約束,清除之前的約束,保留最新的約束,如果想深入解釋下,可以閱讀下面的英文解釋~

/**
 *  Creates a MASConstraintMaker with the callee view.
 *  Any constraints defined are added to the view or the appropriate superview once the block has finished executing
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return Array of created MASConstraints
 */
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
 
/**
 *  Creates a MASConstraintMaker with the callee view.
 *  Any constraints defined are added to the view or the appropriate superview once the block has finished executing.
 *  If an existing constraint exists then it will be updated instead.
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return Array of created/updated MASConstraints
 */
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
 
/**
 *  Creates a MASConstraintMaker with the callee view.
 *  Any constraints defined are added to the view or the appropriate superview once the block has finished executing.
 *  All constraints previously installed for the view will be removed.
 *
 *  @param block scope within which you can build up the constraints which you wish to apply to the view.
 *
 *  @return Array of created/updated MASConstraints
 */
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

2.設(shè)置高度,這里設(shè)置左右邊距,因此不設(shè)置寬度,如果想單獨(dú)設(shè)置width可參考高度的設(shè)置方式:

UIView  *childView=[UIView new];
[childView setBackgroundColor:[UIColor greenColor]];
//先將子View加入在父視圖中
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
    //距離頂部44
    make.top.equalTo(weakSelf.view.mas_top).with.offset(44);
    //距離左邊30
    make.left.equalTo(weakSelf.view.mas_left).with.offset(30);
    //距離右邊30,注意是負(fù)數(shù)
    make.right.equalTo(weakSelf.view.mas_right).with.offset(-30);
    //高度150
    make.height.mas_equalTo(@150);
}];

3.子視圖之間的位置設(shè)置:

UIView  *childView=[UIView new];
[childView setBackgroundColor:[UIColor greenColor]];
//先將子View加入在父視圖中
[self.view addSubview:childView];
__weak typeof(self) weakSelf = self;
[childView mas_makeConstraints:^(MASConstraintMaker *make) {
    //距離頂部44
    make.top.equalTo(weakSelf.view.mas_top).with.offset(44);
    //距離左邊30
    make.left.equalTo(weakSelf.view.mas_left).with.offset(30);
    //距離右邊30,注意是負(fù)數(shù)
    make.right.equalTo(weakSelf.view.mas_right).with.offset(-30);
    //高度150
    make.height.mas_equalTo(@150);
}];
//地址:http://www.cnblogs.com/xiaofeixiang/
UIView *nextView=[UIView new];
[nextView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:nextView];
[nextView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(childView.mas_bottom).with.offset(30);
    make.right.equalTo(childView.mas_right).with.offset(-30);
    make.width.mas_equalTo(@100);
    make.height.mas_equalTo(@100);
}];

4.鏈?zhǔn)綄?xiě)法,算是一個(gè)便利的寫(xiě)法:

    UIView  *childView=[UIView new];
    [childView setBackgroundColor:[UIColor greenColor]];
    //先將子View加入在父視圖中
    [self.view addSubview:childView];
    __weak typeof(self) weakSelf = self;
    [childView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(weakSelf.view).with.offset(100);
        make.bottom.and.right.mas_equalTo(weakSelf.view).with.offset(-100);
        //第二種寫(xiě)法更簡(jiǎn)單,相對(duì)于就是父視圖
//        make.top.and.left.mas_equalTo(100);
//        make.bottom.and.right.mas_equalTo(-100);
    }];
     
    UILabel *label=[UILabel new];
    [label setText:@"博客園-FlyElephant"];
    [label setTextColor:[UIColor redColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.view).with.offset(10);
        make.height.mas_equalTo(20);
        make.right.mas_equalTo(weakSelf.view).with.offset(-10);
        make.bottom.mas_equalTo(weakSelf.view).with.offset(-50);
    }];

 網(wǎng)上關(guān)于Masonry的教程很多,給的例子的也很多,這幾種情況基本上滿足了開(kāi)發(fā)中的需求,不會(huì)有太多的出入,算是一個(gè)簡(jiǎn)易版的教程,Masonry的中屬性和iOS中的屬性是有對(duì)應(yīng)的關(guān)系,不過(guò)因?yàn)楹芎?jiǎn)單,基本上沒(méi)怎么看,下圖是一個(gè)對(duì)照關(guān)系:

總結(jié):

  1. 可以給控件添加left/right/top/bottom/size/height/width/insert約束;
  2. 庫(kù)提供了三個(gè)方法,mas_makeConstraints添加約束,mas_updateConstraints修改約束,mas_remakeConstraints清除以前約束并添加新約束;
  3. 可以通過(guò)view.mas_bottom獲得view的某個(gè)約束;
  4. 在約束的block中,使用make來(lái)給當(dāng)前控件添加約束。

相關(guān)文章

  • iOS實(shí)現(xiàn)圓角箭頭視圖

    iOS實(shí)現(xiàn)圓角箭頭視圖

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)圓角箭頭視圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法

    iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法

    本篇文章主要介紹了iOS11 下載之?dāng)帱c(diǎn)續(xù)傳的bug的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-11-11
  • iOS屏幕適配開(kāi)發(fā)實(shí)用技巧

    iOS屏幕適配開(kāi)發(fā)實(shí)用技巧

    這篇文章主要為大家詳細(xì)介紹了iOS屏幕適配開(kāi)發(fā)實(shí)用技巧,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS Runtime詳解(新手也看得懂)

    iOS Runtime詳解(新手也看得懂)

    這篇文章主要給大家介紹了關(guān)于iOS Runtime的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • iOS開(kāi)發(fā)之UIMenuController使用示例詳解

    iOS開(kāi)發(fā)之UIMenuController使用示例詳解

    這篇文章主要為大家介紹了iOS開(kāi)發(fā)之UIMenuController使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • iOS沙盒視頻縮略圖及保存本地代碼

    iOS沙盒視頻縮略圖及保存本地代碼

    這篇文章主要為大家詳細(xì)介紹了iOS沙盒視頻縮略圖及保存本地的代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • IOS10 遠(yuǎn)程推送適配詳細(xì)介紹

    IOS10 遠(yuǎn)程推送適配詳細(xì)介紹

    這篇文章主要介紹了IOS10 遠(yuǎn)程推送適配詳細(xì)介紹的相關(guān)資料,iOS10推送新增了UserNotifications Framework,這里主要介紹如何實(shí)現(xiàn)遠(yuǎn)程推送,需要的朋友可以參考下
    2016-12-12
  • iOS16使用SwiftUI Charts創(chuàng)建折線圖實(shí)現(xiàn)實(shí)例

    iOS16使用SwiftUI Charts創(chuàng)建折線圖實(shí)現(xiàn)實(shí)例

    這篇文章主要為大家介紹了iOS16使用SwiftUI Charts創(chuàng)建折線圖實(shí)現(xiàn)實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • iOS基于CATransition實(shí)現(xiàn)翻頁(yè)、旋轉(zhuǎn)等動(dòng)畫(huà)效果

    iOS基于CATransition實(shí)現(xiàn)翻頁(yè)、旋轉(zhuǎn)等動(dòng)畫(huà)效果

    這篇文章主要為大家詳細(xì)介紹了iOS基于CATransition實(shí)現(xiàn)翻頁(yè)、旋轉(zhuǎn)等動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • iOS 對(duì)NSMutableArray進(jìn)行排序和過(guò)濾的實(shí)例

    iOS 對(duì)NSMutableArray進(jìn)行排序和過(guò)濾的實(shí)例

    下面小編就為大家分享一篇iOS 對(duì)NSMutableArray進(jìn)行排序和過(guò)濾的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論

宁城县| 桂平市| 开阳县| 饶阳县| 新闻| 武义县| 长沙县| 钟祥市| 诏安县| 泗水县| 泰州市| 乌兰浩特市| 泗洪县| 海兴县| 呈贡县| 沂源县| 松溪县| 和硕县| 千阳县| 化州市| 邛崃市| 克东县| 石景山区| 岳普湖县| 伊宁市| 上栗县| 黄大仙区| 赤水市| 邢台市| 阳朔县| 铁岭县| 边坝县| 宜春市| 葵青区| 成武县| 射洪县| 九龙城区| 嘉善县| 秦安县| 泰州市| 金乡县|