浅析AutoLayout和Masnory之二

来源:互联网 发布:网络兼职英语教师招聘 编辑:程序博客网 时间:2024/06/05 17:07

AutoLayout关于更新的几个方法的区别
setNeedsLayout:告知页面需要更新,但是不会立刻开始更新。执行后会立刻调用layoutSubviews。
layoutIfNeeded:告知页面布局立刻更新。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
layoutSubviews:系统重写布局
setNeedsUpdateConstraints:告知需要更新约束,但是不会立刻开始
updateConstraintsIfNeeded:告知立刻更新约束
updateConstraints:系统更新约束
Masonry使用注意事项
用mas_makeConstraints的那个view需要在addSubview之后才能用这个方法
mas_equalTo适用数值元素,equalTo适合多属性的比如make.left.and.right.equalTo(self.view)
方法and和with只是为了可读性,返回自身,比如make.left.and.right.equalTo(self.view)和make.left.right.equalTo(self.view)是一样的。
因为iOS中原点在左上角所以注意使用offset时注意right和bottom用负数。
AutoLayout情况如何计算UITableView的变高高度
主要是UILabel的高度会有变化,所以这里主要是说说label变化时如何处理,设置UILabel的时候注意要设置preferredMaxLayoutWidth这个宽度,还有ContentHuggingPriority为UILayoutPriorityRequried

CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 10 * 2;textLabel = [UILabel new];textLabel.numberOfLines = 0;textLabel.preferredMaxLayoutWidth = maxWidth;[self.contentView addSubview:textLabel];[textLabel mas_makeConstraints:^(MASConstraintMaker *make) {    make.top.equalTo(statusView.mas_bottom).with.offset(10);    make.left.equalTo(self.contentView).with.offset(10);    make.right.equalTo(self.contentView).with.offset(-10);    make.bottom.equalTo(self.contentView).with.offset(-10);}];[_contentLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];

如果版本支持最低版本为iOS 8以上的话可以直接利用UITableViewAutomaticDimension在tableview的heightForRowAtIndexPath直接返回即可。

tableView.rowHeight = UITableViewAutomaticDimension;tableView.estimatedRowHeight = 80; //减少第一次计算量,iOS7后支持- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    // 只用返回这个!    return UITableViewAutomaticDimension;}

但如果需要兼容iOS 8之前版本的话,就要回到老路子上了,主要是用systemLayoutSizeFittingSize来取高。步骤是先在数据model中添加一个height的属性用来缓存高,然后在table view的heightForRowAtIndexPath代理里static一个只初始化一次的Cell实例,然后根据model内容填充数据,最后根据cell的contentView的systemLayoutSizeFittingSize的方法获取到cell的高。具体代码如下

//在model中添加属性缓存高度@interface DataModel : NSObject@property (copy, nonatomic) NSString *text;@property (assign, nonatomic) CGFloat cellHeight; //缓存高度@end- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    static CustomCell *cell;    //只初始化一次cell    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomCell class])];    });    DataModel *model = self.dataArray[(NSUInteger) indexPath.row];    [cell makeupData:model];    if (model.cellHeight <= 0) {        //使用systemLayoutSizeFittingSize获取高度        model.cellHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;    }    return model.cellHeight;}

动画
因为布局约束就是要脱离frame这种表达方式的,可是动画是需要根据这个来执行,这里面就会有些矛盾,不过根据前面说到的布局约束的原理,在某个时刻约束也是会被还原成frame使视图显示,这个时刻可以通过layoutIfNeeded这个方法来进行控制。具体代码如下

[aniView mas_makeConstraints:^(MASConstraintMaker *make) {    make.top.bottom.left.right.equalTo(self.view).offset(10);}];[aniView mas_updateConstraints:^(MASConstraintMaker *make) {    make.top.equalTo(self.view).offset(30);}];[UIView animateWithDuration:3 animations:^{    [self.view layoutIfNeeded];}];
0 0
原创粉丝点击