Masonry 使用 纯代码画面

首先下载

Masonry 是一个很方便的框架, 用来给控件做约束.

它的语法很直观, 就跟一句英语没什么区别.

Masonry 简单认识

Masonry 支持的约束 :

MASConstraintMaker.h文件中

/**
 * The following properties return a new MASViewConstraint
 *  with the first item set to the makers associated view and the appropriate MASViewAttribute
 */
@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;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
@property (nonatomic, strong, readonly) MASConstraint *baseline;
name 用中国话说
left 左侧
top 上侧
right 右侧
bottom 下侧
leading 首侧
trailing 尾侧
width 宽度
height 高度
centerX X方向中点
centerY Y方向中点
baseline 基线

导入到工程

  • 直接在github上下载, 然后把Masonry文件夹拖拽到工程中.
  • 使用cocoapods

github地址 : https://github.com/Masonry/Masonry

懒得去的话 : git clone https://github.com/SnapKit/Masonry.git

使用

练习1: 使一个控件居中

导入 Masonry.h 头文件.

- (void)createView {

    // typeof 一个weak引用的self, 方便在block中使用
    __weak typeof(self) weakSelf = self;

    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:view];
    // 这里要注意, 使用masonry的时候, 要先把控件加到父视图上, 再进行约束
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        // 设置view居中
        make.center.equalTo(weakSelf.view);
        // 设置size
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
}

如果在添加父视图之前进行约束, 就会得到这个异常 :

Terminating app due to uncaught exception ‘NSInternalInconsistencyException‘, reason: ‘couldn‘t find a common superview for <UIView: 0x7f92cb61c400; frame = (0 0; 0 0); layer = <CALayer: 0x7f92cb60f6d0>> and <UIView: 0x7f92cb60c8b0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7f92cb609740>>‘

Note. 定义一个weakSelf MACRO

#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

所谓的 链式语法 这里还不是太明显 ...

一个 block 指定一个控件的 frame. 在任何设备上都好用, 这就是 Masonry 的作用.

效果如下 : (话说这图也太大了)

center

Masonry 中给控件设置约束的3个函数

上面用到了 mas_makeConstraints: 这个方法, 从名字可以看出来这是为控件设置约束用的方法, 这样的方法:

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

mas_makeConstraints: 只负责新增约束, Autolayout不能同时存在两条针对于同一对象的约束, 否则会报错.

mas_updateConstraints: 针对上面的情况, 会更新在block中出现的约束, 不会导致出现两个相同约束的情况.

mas_remakeConstraints: 则会清除之前的所有约束, 仅保留最新的约束.

mas_equalTo

#define mas_equalTo(...) equalTo(MASBoxValue((__VA_ARGS__)))

#define MASBoxValue(value) _MASBoxValue(@encode(__typeof__((value))), (value))

mas_equelTo() 括号里放具体的数字或者边界(top, left ...)

equelTo() 括号里放对象(view, 约束什么的)

_MASBoxValue()是一个C函数, 功能如下:

/**
 *  Given a scalar or struct value, wraps it in NSValue
 *  Based on EXPObjectify: https://github.com/specta/expecta
 */

练习2: 在一个控件中布局其他控件(单个)

在刚才的view中, 添加一个view, 使这个view居中, 大小略小.

    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor blackColor];
    [view addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view).with.offset(10);
        make.bottom.equalTo(view).with.offset(-10);
        make.left.equalTo(view).with.offset(10);
        make.right.equalTo(view).with.offset(-10);
    }];

这里使用Masonry设置约束有几种方式, 可以设置top bottom left right, 也可以使用如下设置:

    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(view).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];

这里链式语法就能够体现出来了, 就和一个英文句子一样, 约束好了一个view.

上面那种, 是用绝对的距离, 也就是坐标点的差. 下面这种, 利用了系统的UIEdge是对上左下右进行设置, 是相对距离. 其中的差别就是体现在参数的正负上.

这里有个很有意思的地方: withand

- (MASConstraint *)with {
    return self;
}
- (MASConstraint *)and {
    return self;
}

这两个函数, 就是为了配合链式语法写的. 使用的时候有没有都是一样的.

练习3: 在一个控件中布局其他控件(两个)

要求: 在一个view中, 约束两个view, 大小一致, 并排居中.

    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor whiteColor];
    [view1 addSubview:view2];

    UIView *view3 = [[UIView alloc] init];
    view3.backgroundColor = [UIColor whiteColor];
    [view1 addSubview:view3];

    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view1.mas_centerY);
        make.left.equalTo(view1).with.offset(10);
        make.right.equalTo(view3.mas_left).with.offset(-10);
        make.height.mas_equalTo(@100);
        make.width.equalTo(view3);
    }];

    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(view1.mas_centerY);
        make.left.equalTo(view2.mas_right).with.offset(10);
        make.right.equalTo(view1).with.offset(-10);
        make.height.mas_equalTo(@100);
        make.width.equalTo(view2);
    }];

效果如下图:

链接:https://www.jianshu.com/p/6f409c57e75e

原文地址:https://www.cnblogs.com/lanmaokomi/p/8657803.html

时间: 2024-08-29 05:58:22

Masonry 使用 纯代码画面的相关文章

Masonry -- 使用纯代码进行iOS应用的autolayout自适应布局

快速入门 安装 使用 CocoaPods 安装 pod 'Masonry' 推荐在你的在 prefix.pch 中引入头文件: // 定义这个常量,就可以在使用Masonry不必总带着前缀 `mas_`: #define MAS_SHORTHAND // 定义这个常量,以支持在 Masonry 语法中自动将基本类型转换为 object 类型: #define MAS_SHORTHAND_GLOBALS #import "Masonry.h" 使用 初始Masonry 这是使用MASCo

通过 Masonry使用纯代码方式编写 Auto Layout--配置 Masonry

Auto Layout 很 awesome 对不对,但是apple 默认的用代码写 Auto Layout 的方式实在是太鸡肋了-噩梦一样! 但是作为一个手(也)码(爱)爱(装)好(X)者,用 StoryBoard 显然不是咱的菜! 对了,忘了说一句, SizeClass 很酷,并且是用 IB 来弄的,但是先不用担心,我们至少要适配 iOS7,也就是说, Size Class 到很流行还有一段时间,到时候肯定有大神(或者是你)找出 Size Class 的代码解决方案的 ! 言归正传,我们回到代

第三方框架-纯代码布局:Masonry的简单使用

Masonry是一个对系统NSLayoutConstraint进行封装的第三方自动布局框架,采用链式编程的方式提供给开发者API.系统AutoLayout支持的操作,Masonry都支持,相比系统API功能来说,Masonry是有过之而无不及. Masonry采取了链式编程的方式,代码理解起来非常清晰易懂,而且写完之后代码量看起来非常少.之前用NSLayoutConstraint写很多代码才能实现的布局,用Masonry最少一行代码就可以搞定.下面看到Masonry的代码就会发现,太简单易懂了.

(源码推荐)快速开发10几天时间纯代码高仿《内涵段子》

这个项目是用OC编写,如果有的朋友已经下载下来看了这个项目, 就会意识到这个项目没有一个storyboard或者是nib,不是因为不喜欢用storyboard或者nib,而是因为一直以来就想用纯代码写个项目,(好远大的梦想..开玩笑的..),但是项目是写出来的,光想不做不写是不行的,所以我就开始我的”内涵之旅“了. Github 地址 https://github.com/Charlesyaoxin/NeiHanDuanZI 日志: 8.30号:没怎么做东西,就是搭建了项目的架构,拉入了之前经常

iOS界面布局之三——纯代码的autoLayout及布局动画

iOS界面布局之三--纯代码的autoLayout及布局动画 一.引言 关于界面布局,apple的策略已经趋于成熟,autolayout的优势在开发中也已经展现的淋漓尽致.除了使用storyBoard进行布局约束的拖拽,有时我们也需要在代码中进行autolayout的布局设置,Masonry库可以方便的创建约束属性,实际上,我们也没有必要再使用系统原生的代码来创建和设置约束,这篇博客只作为使用的方法备忘.前几篇布局介绍的链接如下: 使用autoresizing进行界面布局:http://my.o

autoLyout纯代码适配

前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 window的size固定为(320,480) 我们只需要简单计算一下相对位置就好了 在iphone4-iphone4s时代 苹果推出了retina屏 但是给了码农们非常大的福利:window的size不变 在iphone5-iphone5s时代 window的size变了(320,568) 这时auto

纯代码Autolayout的三种方法

Autolayout讲解较多的就是xib和storyboard用法,本文主要记录纯代码的Autolayout使用方法: 方法1.苹果原生的方法,这种方法虽然简单但是太过繁杂,可用性很差 //宽度=superView高度 [superView addConstraint:[NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toIte

史上比较用心的纯代码实现 AutoLayout

入职有两三个月了吧,都是使用 Objective-C 纯代码(虽然有时候偷偷参杂一些 Swift 开源库)来编写公司APP,写布局的时候几乎都是要么在初始化的时候用 initWithFrame,要么就初始化完毕之后用 view.frame.虽然这种方法很直观,一眼就可以看出这个 view 的位置以及大小,但是坏处也是有的,比如说在计算的时候麻烦等等. 概述 使用 Objective-C 纯代码编写 AutoLayout,看 AutoLayout 的字面理解就是自动布局,听起来好像蛮屌的样子.说白

纯代码 自动屏幕适配iPhone

代码判断,你也可以用xib自带的自动布局选项 我是用的纯代码写的 纯代码 自动屏幕适配iPhone,布布扣,bubuko.com