iOS自适应(代码) 简单介绍一下

从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上



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值