从iphone4,4s,到iphone5,5s,屏幕宽度没变,所以我们对于自适应没怎么在意,大部分页面也都是tableView,所以最多就是把先前写死的高,改成动态的
但是当iphone5-iphone6,6s就跪了,宽度也变了,而且图片还需要三倍的,xib有autoLayout
代码出了一个masonry
其实我们简单的用一些autoresizingMask(
UIViewAutoresizingFlexibleLeftMargin = 1 << 0,
UIViewAutoresizingFlexibleWidth = 1 << 1,
UIViewAutoresizingFlexibleRightMargin = 1 << 2,
UIViewAutoresizingFlexibleTopMargin = 1 << 3,
UIViewAutoresizingFlexibleHeight = 1 << 4,
UIViewAutoresizingFlexibleBottomMargin = 1 << 5
)即可接着也有个第三方的叫masonry的出现了,可以去github上下了看看
我们会看到他有一些属性
@property (nonatomic, strong, readonly) id<MASConstraint> left;
@property (nonatomic, strong, readonly) id<MASConstraint> top;
@property (nonatomic, strong, readonly) id<MASConstraint> right;
@property (nonatomic, strong, readonly) id<MASConstraint> bottom;
@property (nonatomic, strong, readonly) id<MASConstraint> leading;
@property (nonatomic, strong, readonly) id<MASConstraint> trailing;
@property (nonatomic, strong, readonly) id<MASConstraint> width;
@property (nonatomic, strong, readonly) id<MASConstraint> height;
@property (nonatomic, strong, readonly) id<MASConstraint> centerX;
@property (nonatomic, strong, readonly) id<MASConstraint> centerY;
@property (nonatomic, strong, readonly) id<MASConstraint> baseline;
其实leading和left,trailing和right是等价的
我们一般用到属性就这些
再来看看语法,这些属性怎么用
[topView makeConstraints:^(MASConstraintMaker *make) {
UIView *topLayoutGuide = (id)self.topLayoutGuide;
make.top.equalTo(topLayoutGuide.bottom);
make.left.equalTo(self.view);
make.right.equalTo(self.view);
make.height.equalTo(@40);
}];
看到他example里的语句这是topView相对于topLayoutGuide做的一些约束
应该很好理解
添加约束一共有三个方法
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;
makeConstraints只负责新增约束,不能同时存在两条对于同一对象的约束,否则会报错
updateConstraints更新在block中出现的约束
不过要记住,在添加约束前,view必须已经被addSubView 到superview上