iOS

Masonry 自动适配,清除Masonry中的警告

2016-05-04  本文已影响1684人  6129b93b59e2

关于Masonry的用法 我强烈推荐@"里脊串"的两边文章
http://adad184.com/2014/09/28/use-masonry-to-quick-solve-autolayout/
http://adad184.com/2015/06/08/complex-cell-with-masonry/

那用法我就不多介绍了

我们的项目中通常会用到tabbleview,那就避免不了会用到cell,以前我们会去计算cell 的高度。现在我们都用约束,我又不想去算cell的高度,怎么办。
ios8 以后

  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
if (MYDEVICE_SYS_VERSION >= 8.0) {
    return UITableViewAutomaticDimension ;
}
  else
    {
     EBYRecommendCell  * cell = [tableView dequeueReusableCellWithIdentifier:@"EBYRecommendCell"];
    if (cell == nil) {
        cell = [[EBYRecommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EBYRecommendCell"] ;
    }
    
      这里可以把cell的高度缓存,免的每次都去算,我写demo 懒

    [cell setsubModel:[self.listArry objectAtIndex:indexPath.row]];
    [cell setNeedsUpdateConstraints] ;
    [cell updateConstraintsIfNeeded] ;
    [cell setNeedsLayout] ;
    [cell layoutIfNeeded] ;
    return [cell.contentView  systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height ;
    }
  }
Simulator Screen Shot 2016年5月4日 上午11.47.07.png

比如说最上面的是cell cell里面有一张图片 我们约束了top left size bottom
但是这个时候 masonry就会给你警告

  ** Unable to simultaneously satisfy constraints.**
  ** Probably at least one of the constraints in the following list is one               you don't want. **
  ** Try this: **
  **  (1) look at each constraint and try to figure out which you don't         expect; **
  **  (2) find the code that added the unwanted constraint or constraints       and fix it. **
  **(**
  **    "<MASLayoutConstraint:0x7ff8c259f3c0 UIView:0x7ff8c274e430.top == UITableViewCellContentView:0x7ff8c2754920.top>",**
    **    "<MASLayoutConstraint:0x7ff8c4851d40 UIView:0x7ff8c274e430.bottom == UITableViewCellContentView:0x7ff8c2754920.bottom>",**
   **    "<MASLayoutConstraint:0x7ff8c2757710 UIImageView:0x7ff8c2755880.top == UIView:0x7ff8c274e430.top + 12.144>",**
   **    "<MASLayoutConstraint:0x7ff8c2757e00 UIImageView:0x7ff8c2755880.height == 55.2>",**
   **    "<MASLayoutConstraint:0x7ff8c2756740 UIImageView:0x7ff8c2755880.bottom == UIView:0x7ff8c274e430.bottom - 12.144>",**
   **    "<NSLayoutConstraint:0x7ff8c275a100 UITableViewCellContentView:0x7ff8c2754920.height == 79.3333>"**
   **)**

他的意思是说 你的约束重复了,请检查下 看看是否能找到,然后修改他,

  [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {

  //            make.top.equalTo(self.leftBottomView.mas_top).offset(22/2).priorityHigh();
  //            make.left.equalTo(self.leftBottomView.mas_left).offset(34/2).priorityHigh();
  //            make.size.mas_equalTo(CGSizeMake(50 * RATIO, 50 * RATIO)).priorityHigh();
  //            make.bottom.equalTo(self.leftBottomView.mas_bottom).offset(-22/2).priorityHigh();
        
        
        make.top.equalTo(self.leftBottomView.mas_top).offset(22/2);
        make.left.equalTo(self.leftBottomView.mas_left).offset(34/2);
        make.size.mas_equalTo(CGSizeMake(50 * RATIO, 50 * RATIO));
        make.bottom.equalTo(self.leftBottomView.mas_bottom).offset(-22/2);
    }];

上面写了两种写法的约束,他们之间就差了 priorityHigh
这个是优先级 优先级分三种,高中低,我们默认给的约束都是中,所以它会产生一些歧义,这个时候你要手动的给他划分优先级。快去试试清除项目中的警告吧~

上一篇下一篇

猜你喜欢

热点阅读