iOS Masonry介绍与使用实践:快速上手Autolayout

如题,今天的任务是Nasonry介绍与使用实践:快速上手Autolayout & 本身App项目的适配 & 遇到的问题及解决方法。布局

Tips: .h与.m文件之间的切换快捷键 control + command + 上下键ui

Tips:如何在Xib中设置Button的属性(圆角以及背景颜色)。Inspector -- User Defined Runtime Attributes -- layer.cornerRadius & Number & 10spa

9月14号更新--好吧,上周五的任务成功推迟到了今天,今天我们来会会它。.net

    //mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 不然会报错
    - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
    //mas_remakeConstraints 则会清除以前的全部约束 仅保留最新的约束
    - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
    //mas_updateConstraints 针对上面的状况 会更新在block中出现的约束 不会致使出现两个相同约束的状况
    - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

 首先是如何在项目中引用第三方库的文件。即便用cocoaPods import导入时没有提示的解决办法:ssr

选择target--> Building Settings-->User Header Search Paths -->在后面的空白处点击。左侧的写入 $(PODS_ROOT) -- >后面的选择rucursive。便可。code

 

好了,能够调用了。对象

问了下大神,技能get时间。blog

好比说我若是想把这张图片放在sele.view的中心,而后距离top50像素处,那么代码应该这么写。图片

    UIImageView *heads = [[UIImageView alloc] init];
    heads.layer.masksToBounds = YES;
    heads.layer.cornerRadius = 27;//圆角半径?
    [heads setImage:[UIImage imageNamed:@"user_head"]];
    [self.view addSubview:heads];
    [heads mas_makeConstraints:^(MASConstraintMaker *make){

        make.top.equalTo(self.view).with.offset(30);
        make.centerX.equalTo(self.view);
    }];

这里须要注意的是,自动布局是相对于superView和兄弟view的,不能相对本身来布局ip

因此代码中括号里面的是self.view,而不是heads。

---2015-09-15更新

 上面所说的三种方法,昨天只用到了其中一种,即mas_makeConstraints。

估计本身只摸索着更表面的东西。上一个例子吧。

[loginButton mas_makeConstraints:^(MASConstraintMaker *make){
        
        make.left.right.and.width.equalTo(self.view);
        make.top.equalTo(centerLabel.mas_bottom).with.offset(30);
        make.height.equalTo(@50);
    }];

像这种特别简单,只须要记住一点,Masonry实现的autolayout这种布局方式是相对于superView以及它的兄弟view的,千万不能想对本身来布局。

相关文章
相关标签/搜索