从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上
自适应布局与Masonry框架在iOS开发中的应用
本文探讨了在iOS设备从iPhone 4系列升级到iPhone 5系列后,屏幕尺寸变化带来的自适应布局挑战。通过介绍autoresizingMask属性和Masonry框架的使用,展示了如何实现屏幕尺寸自适应布局,包括使用Masonry框架的属性来设置视图约束,以及如何在代码中添加和更新约束。此外,文章还提供了Masonry框架的实例代码和语法说明,帮助开发者更好地理解和应用这一工具。
1万+

被折叠的 条评论
为什么被折叠?



