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

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

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

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

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

    //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导入时没有提示的解决办法:

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

 

好了,可以调用了。

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

比如说我如果想把这张图片放在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的,不能相对自己来布局

所以代码中括号里面的是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的,千万不能想对自己来布局。

posted on 2015-09-15 09:22  Fs_purple  阅读(289)  评论(0编辑  收藏  举报