代码自动布局,及masonry
2016-04-25 11:49
316 查看
/* Create constraints explicitly. Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant" If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute. */ +(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
参数说明:
第一个参数 view1: 要设置的视图;
第二个参数 attr1: view1要设置的属性,稍后详解;
第三个参数 relation: 视图view1和view2的指定属性之间的关系,稍后详解;
第四个参数 view2: 参照的视图;
第五个参数 attr2: 参照视图view2的属性,稍后详解;
第六个参数 multiplier: 视图view1的指定属性是参照视图view2制定属性的多少倍;
第七个参数 c: 视图view1的指定属性需要加的浮点数。
typedef NS_ENUM(NSInteger, NSLayoutAttribute) { NSLayoutAttributeLeft = 1, NSLayoutAttributeRight, NSLayoutAttributeTop, NSLayoutAttributeBottom, NSLayoutAttributeLeading, NSLayoutAttributeTrailing, NSLayoutAttributeWidth, NSLayoutAttributeHeight, NSLayoutAttributeCenterX, NSLayoutAttributeCenterY, NSLayoutAttributeBaseline, NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline, NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0), NSLayoutAttributeNotAnAttribute = 0 };
分三部分解释 NSLayoutAttribute
第一部分:常用的
NSLayoutAttributeLeft: CGRectGetMinX(view.frame);
NSLayoutAttributeRight: CGRectGetMaxX(view.frame);
NSLayoutAttributeTop: CGRectGetMinY(view.frame);
NSLayoutAttributeBottom: CGRectGetMinY(view.frame);
NSLayoutAttributeWidth: CGRectGetWidth(view.frame);
NSLayoutAttributeHeight: CGRectGetHeight(view.frame);
NSLayoutAttributeCenterX: view.center.x;
NSLayoutAttributeCenterY:view.center.y ;
NSLayoutAttributeBaseline: 文本底标线,在大多数视图中等同于NSLayoutAttributeBottom; 在少数视图,如UILabel,是指字母的底部出现的位置;
NSLayoutAttributeLastBaseline: 相当于NSLayoutAttributeBaseline;
NSLayoutAttributeFirstBaseline: 文本上标线;
NSLayoutAttributeNotAnAttribute: None;
第二部分: 根据国家使用习惯不同表示的意思不同
NSLayoutAttributeLeading: 在习惯由左向右看的地区,相当于NSLayoutAttributeLeft;在习惯从右至左看的地区,相当于NSLayoutAttributeRight;
NSLayoutAttributeTrailing: 在习惯由左向右看的地区,相当于NSLayoutAttributeRight;在习惯从右至左看的地区,相当于NSLayoutAttributeLeft;
第三部分:ios8新增属性,各种间距
NSLayoutAttributeLeftMargin,
NSLayoutAttributeRightMargin,
NSLayoutAttributeTopMargin,
NSLayoutAttributeBottomMargin,
NSLayoutAttributeLeadingMargin,
NSLayoutAttributeTrailingMargin,
NSLayoutAttributeCenterXWithinMargins,
NSLayoutAttributeCenterYWithinMargins,
下面拿上两段,自定义View 的适配
UIView *superview = self; UIView *view1 = [[UIView alloc] init]; view1.translatesAutoresizingMaskIntoConstraints = NO; view1.backgroundColor = [UIColor greenColor]; [superview addSubview:view1]; [superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeTop multiplier:1.0 constant:10], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:90], [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:90], ]]; UILabel *lab = [[UILabel alloc] init]; [superview addSubview:lab]; lab.numberOfLines = 0; lab.backgroundColor = [UIColor yellowColor]; lab.translatesAutoresizingMaskIntoConstraints = NO; [superview addConstraints:@[ //view1 constraints [NSLayoutConstraint constraintWithItem:lab attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeTop multiplier:1.0 constant:0], [NSLayoutConstraint constraintWithItem:lab attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:view1 attribute:NSLayoutAttributeRight multiplier:1.0 constant:10], [NSLayoutConstraint constraintWithItem:lab attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10], ]]; lab.text = @"adsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdas";
masonry
UIView *superview = self; UIView *view1 = [[UIView alloc] initWithFrame:CGRectZero]; [superview addSubview:view1]; view1.backgroundColor = [UIColor greenColor]; [view1 mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(superview.mas_top).with.offset(10); make.left.equalTo(superview.mas_left).with.offset(10); make.size.mas_equalTo(CGSizeMake(90, 90)); }]; UILabel *lab = [[UILabel alloc] initWithFrame:CGRectZero]; [superview addSubview:lab]; lab.text = @"adsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdasadsadasdasdas"; lab.numberOfLines = 0; lab.backgroundColor = [UIColor yellowColor]; [lab mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(view1.mas_top).with.offset(0); make.left.equalTo(view1.mas_right).with.offset(10); make.right.equalTo(self.mas_right).with.offset(-10); }];
效果图是一样上面的
讲解一下masonry
相关文章推荐
- JNI中JAVA调用C/C++方法
- C#学习——简单socket、多线程
- python re 模块
- Java语法糖2:自动装箱和自动拆箱
- java中RSA加解密的实现
- Python中的引用和拷贝浅析
- ASP.NET mvc 在视图中使用@helper封装输出代码
- C/C++ volatile让你看的更明白
- C#日期时间格式化
- Webdriver(selenium2.0)+NUnit+C# (一)
- python2.X编码问题梳理
- 多线程中Thread和Runnable的区别
- Spring AOP工作机制
- java 中序列化和反序列化
- Java语法糖1:可变长度参数以及foreach循环原理
- java8 Lambda 表达式
- C#新手学习快速成长学习资源汇总
- C++实验4-项目3:乘法口诀表
- Python简单实现enum功能的方法
- 编程之美扩展问题总结2.1(2)