浅谈Autolayout之Masonry
来源:互联网 发布:最新网络流行词2016 编辑:程序博客网 时间:2024/05/17 17:56
随着iphone大屏时代的到来,ios开发中的界面适配变的不再容易,frame已经变的有点鸡肋。
@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 *width;
@property (nonatomic, strong, readonly) MASConstraint *height;
@property (nonatomic, strong, readonly) MASConstraint *centerX;
@property (nonatomic, strong, readonly) MASConstraint *centerY;
UIView * view = [[UIView alloc] init];
view.frame = CGRectMake(50, 100, 80, 40);
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
UIView * view = [[UIView alloc] init];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
Bself(bself);
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(bself.view).with.offset(50);
make.top.equalTo(bself.view).with.offset(100);
make.width.mas_equalTo(@80);
make.height.mas_equalTo(@40);
}];
下面解释一下
//block里不能直接使用self,要用下面这个宏转换一下(#define Bself(weakSelf) __weak __typeof(&*self)weakSelf = self;)
Bself(bself);
//Autolayout用调用的block
[view mas_makeConstraints:^(MASConstraintMaker *make) {
//距self.view的左边50
make.left.equalTo(bself.view).with.offset(50);
//距self.voew的顶上100
make.top.equalTo(bself.view).with.offset(100);
//宽固定为80
make.width.mas_equalTo(@80);
//高固定为40
make.height.mas_equalTo(@40);
}];
Masonry是用block进行回调,添加约束有三个方法:
- (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 则会清除之前的所有约束 仅保留最新的约束
*/
UIView * view2 = [[UIView alloc] init];
view2.backgroundColor = [UIColor blueColor];
[self.view addSubview:view2];
Bself(bself);
[view2 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(bself.view).with.offset(20);
make.bottom.equalTo(bself.view).with.offset(-20);
make.left.equalTo(bself.view).with.offset(20);
make.right.equalTo(bself.view).with.offset(-20);
/*
等价于
make.edges.equalTo(bself.view).with.insets(UIEdgeInsetsMake(20, 20, 20, 20));
*/
}];
记得右和下是负数,如果是正数的话就变成超出右边或下边20了。效果图如下UIView * view3 = [[UIView alloc] init];
view3.backgroundColor = [UIColor greenColor];
[view2 addSubview:view3];
UIView * view4 = [[UIView alloc] init];
view4.backgroundColor = [UIColor greenColor];
[view2 addSubview:view4];
[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view2).with.offset(20);
make.bottom.equalTo(view4.mas_top).with.offset(-20);
make.width.mas_equalTo(@200);
make.height.equalTo(view4);
}];
[view4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view4.mas_bottom).with.offset(20);
make.bottom.equalTo(view2).with.offset(-20);
make.width.mas_equalTo(@200);
make.height.equalTo(view3);
}];
效果如下[view3 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view2).with.offset(20);
make.bottom.equalTo(view4.mas_top).with.offset(-20);
make.width.mas_equalTo(@200);
//高度是view4两倍
make.height.equalTo(view4).with.multipliedBy(2);
}];
[view4 mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(view2.mas_centerX);
make.top.equalTo(view4.mas_bottom).with.offset(20);
make.bottom.equalTo(view2).with.offset(-20);
make.width.mas_equalTo(@200);
//高度是view3的高度除以2
make.height.equalTo(view3).with.dividedBy(2);
}];
- 浅谈Autolayout之Masonry
- 浅谈Autolayout-04Masonry
- AutoLayout 之 Masonry
- iOS Autolayout之Masonry解读
- iOS Autolayout之Masonry解读
- iOS Autolayout之Masonry解读
- iOS Autolayout之Masonry详解
- Masonry - Autolayout
- Masonry Autolayout
- AutoLayout(Masonry)
- ios开发autolayout之Masonry使用总结
- iOS 一一 AutoLayout之Masonry实现
- ios进阶--Autolayout 之第三方框架--Masonry
- iOS的屏幕适配之Autoresizing、Autolayout、VFL、Masonry
- ios开发之autolayout 第三方框架Masonry
- (0068)iOS开发之AutoLayout框架Masonry使用心得
- Masonry学习之前奏-纯代码实现 AutoLayout
- iOS 的 AutoLayout:Masonry
- CAS和Shiro在spring中集成
- 我的bashrc,留而利值(不定期更新)
- solr 4.7.2 环境搭建
- 模式识别入门与贝叶斯决策理论
- 【LeetCode #179】Largest Number 解题报告
- 浅谈Autolayout之Masonry
- Android一分钟环境搭建,包含Mac、Linux、Windows
- UVa 11292 The Dragon of Loowater 勇者斗恶龙
- valgrind跟踪fork出的子进程-debug调试
- 解决安卓点击图片跳转时候的蓝色边框问题
- sap 去掉小数位没用的0
- uva 1103 详解
- NSString截取字符串
- JFreeChart 2