iOS Masonry的使用须要注意的地方

     自动布局最重要的是约束:UI元素间关系的数学表达式。约束包括尺寸、由优先级和阈值管理的相对位置。它们是添加剂,可能致使约束冲突 、约束不足形成布局没法肯定 。这两种状况都会产生异常。git

使用前:AutoLayout关于更新的几个方法的区别

  • setNeedsLayout:告知页面须要更新,可是不会马上开始更新。执行后会马上调用layoutSubviews。
  • layoutIfNeeded:告知页面布局马上更新。因此通常都会和setNeedsLayout一块儿使用。若是但愿马上生成新的frame须要调用此方法,利用这点通常布局动画能够在更新布局后直接使用这个方法让动画生效。
  • layoutSubviews:系统重写布局
  • setNeedsUpdateConstraints:告知须要更新约束,可是不会马上开始
  • updateConstraintsIfNeeded:告知马上更新约束
  • updateConstraints:系统更新约束

使用

1. 基本使用github

  • mas_makeConstraints:添加约束
  • mas_updateConstraints:更新约束、亦可添加新约束
  • mas_remakeConstraints:重置以前的约束缓存

  • multipler属性表示约束值为约束对象的乘因数, dividedBy属性表示约束值为约束对象的除因数,可用于设置view的宽高比less

 // 进行屏幕的适配的时候,每每须要根据屏幕宽度来适配一个相应的高度,在此推荐使用以下约束的方式来进行控件的适配
    [self.topView addSubview:self.topInnerView];
    [self.topInnerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self.topView.mas_height).dividedBy(3);
        make.width.and.height.lessThanOrEqualTo(self.topView);
        make.width.and.height.equalTo(self.topView).with.priorityLow();
        make.center.equalTo(self.topView);
    }];ide

  • priorityLow()设置约束优先级
  • #define MAS_SHORTHAND_GLOBALS使用全局宏定义,可使equalTo等效于mas_equalTo
  • #define MAS_SHORTHAND使用全局宏定义, 能够在调用masonry方法的时候不使用mas_前缀
// 这里注意到一个地方,就是当使用了这个全局宏定义以后,发现能够有个类`NSArray+MASAdditions.h`,看了以后发现能够 self.buttonViews = @[ raiseButton, lowerButton, centerButton ]; // 以后能够在updateConstraints 方法中 - (void)updateConstraints { [self.buttonViews updateConstraints:^(MASConstraintMaker *make) { make.baseline.equalTo(self.mas_centerY).with.offset(self.offset); }]; [super updateConstraints]; }

动态修改视图约束:布局

// 建立视图约束 [blueView mas_makeConstraints:^(MASConstraintMaker *make) { self.animatableConstraint = make.edges.equalTo(superview).insets(paddingInsets).priorityLow(); ]]; // 更改约束 (另外一处方法中) UIEdgeInsets paddingInsets = UIEdgeInsetsMake(padding, padding, padding, padding); self.animatableConstraint.insets = paddingInsets; [self layoutIfNeeded];
  • debug模式:
    // 对某个view添加key值 greenView.mas_key = @"greenView"; // 或者以下顺序 MASAttachKeys(greenView, redView, blueView, superview); // 一样的对每条约束亦能够添加key make.height.greaterThanOrEqualTo(@5000).key(@"ConstantConstraint");
  • preferredMaxLayoutWidth: 多行label的约束问题
// 已经确认好了位置 // 在layoutSubviews中确认label的preferredMaxLayoutWidth值 - (void)layoutSubviews { [super layoutSubviews]; // 你必须在 [super layoutSubviews] 调用以后,longLabel的frame有值以后设置preferredMaxLayoutWidth self.longLabel.preferredMaxLayoutWidth = self.frame.size.width-100; // 设置preferredLayoutWidth后,须要从新布局 [super layoutSubviews]; }
  • scrollView使用约束的问题:原理经过一个contentView来约束scrollView的contentSize大小,也就是说以子控件的约束条件,来控制父视图的大小
// 1. 控制scrollView大小(显示区域) [self.scrollView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.view); }]; // 2. 添加一个contentView到scrollView,而且添加好约束条件 [contentView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView); // 注意到此处的宽度约束条件,这个宽度的约束条件是比添加项 make.width.equalTo(self.scrollView); }]; // 3. 对contentView的子控件作好约束,达到能够控制contentView的大小
  • 新方法:2个或2个以上的控件等间隔排序
/** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fixedSpacing 间隔大小 * @param leadSpacing 头部间隔 * @param tailSpacing 尾部间隔 */ - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedSpacing:(CGFloat)fixedSpacing l eadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing; /** * 多个固定大小的控件的等间隔排列,变化的是间隔的空隙 * * @param axisType 轴线方向 * @param fixedItemLength 每一个控件的固定长度或者宽度值 * @param leadSpacing 头部间隔 * @param tailSpacing 尾部间隔 */ - (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;

使用方法很简单,由于它是NSArray的类扩展:动画

// 建立水平排列图标 arr中放置了2个或连个以上的初始化后的控件 // alongAxis 轴线方向 固定间隔 头部间隔 尾部间隔 [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:5 tailSpacing:5]; [arr makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(@60); make.height.equalTo(@60); }];

2. 注意事项ui

  • 约束视图对象只有在被addSubview以后,才能给视图添加约束
  • 当你的全部约束都在 updateConstraints 内调用的时候,你就须要在此调用此方法,由于 updateConstraints方法是须要触发的
// 调用在view 内部,而不是viewcontroller + (BOOL)requiresConstraintBasedLayout { return YES; } /** * 苹果推荐 约束 增长和修改 放在此方法种 */ - (void)updateConstraints { [self.growingButton updateConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self); make.width.equalTo(@(self.buttonSize.width)).priorityLow(); make.height.equalTo(@(self.buttonSize.height)).priorityLow(); make.width.lessThanOrEqualTo(self); make.height.lessThanOrEqualTo(self); }]; //最后记得回调super方法 [super updateConstraints]; }
  • 若是想要约束变换以后实现动画效果,则须要执行以下操做
// 通知须要更新约束,可是不当即执行 [self setNeedsUpdateConstraints]; // 当即更新约束,以执行动态变换 // update constraints now so we can animate the change [self updateConstraintsIfNeeded]; // 执行动画效果, 设置动画时间 [UIView animateWithDuration:0.4 animations:^{ [self layoutIfNeeded]; }];

自动计算UITableViewCell高度

推荐使用一个库UITableView-FDTemplateLayoutCellatom

想法:动态高度的cell, 主要关注的点是什么?spa

  • viewController自己不须要知道cell的类型
  • cell的高度与viewController没有相关性,cell的高度由cell自己来决定
  • viewController真正作到的是一个

如下是摘抄过来的

主要是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; }
相关文章
相关标签/搜索