Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10.3 系统,Label高度自适应不行了 #291

Closed
leizGit opened this issue Mar 31, 2017 · 30 comments
Closed

10.3 系统,Label高度自适应不行了 #291

leizGit opened this issue Mar 31, 2017 · 30 comments

Comments

@leizGit
Copy link

leizGit commented Mar 31, 2017

设置preferredMaxLayoutWidth 可以解决,但是如果是使用自动布局,一般我们不知道Label的宽度

@SiTianyu
Copy link

SiTianyu commented Apr 1, 2017

确实,求作者解决

@zp18862956943
Copy link

啊,希望作者能看到啊,10.3的确是无法高度自适应了

@lovemo
Copy link

lovemo commented Apr 5, 2017

已经有解决方法, 在cell里进行布局之前先对contentView进行约束就可以了,我的是这样解决的
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }];

@anyuhao
Copy link

anyuhao commented Apr 5, 2017

有约束冲突,需要设置preferredMaxLayoutWidth
会报UITableViewCellContentView .width = 375
与 UITableViewCellContentView .width = 0 冲突.
如果返回的高度写死就没有冲突了

@somson
Copy link

somson commented Apr 5, 2017

如果xib布局中设置cell的高度为350,如果[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }];这样解决的话,在iPhone6中会报UITableViewCellContentView .width = 375
与 UITableViewCellContentView .width = 350 冲突.

@leizGit
Copy link
Author

leizGit commented Apr 5, 2017

@lovemo 设置contentView的约束,label显示没问题,但是其他约束出现了问题

@JiaLiangoooo
Copy link

直接在 awakeFromNib 那里 把 preferredMaxLayoutWidth 写进去不可以吗?

@ChenYanTao666
Copy link

  • (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id))configuration
    {
    if (!identifier) {
    return 0;
    }

    // Fetch a cached template cell for identifier.
    UITableViewCell *cell = [self fd_templateCellForReuseIdentifier:identifier];

    // Manually calls to ensure consistent behavior with actual cells (that are displayed on screen).
    [cell prepareForReuse];

    // Customize and provide content for our template cell.
    if (configuration) {
    configuration(cell);
    }

    CGFloat contentViewWidth = CGRectGetWidth(self.frame);

    // If a cell has accessory view or system accessory type, its content view's width is smaller
    // than cell's by some fixed values.
    if (cell.accessoryView) {
    contentViewWidth -= 16 + CGRectGetWidth(cell.accessoryView.frame);
    } else {
    static CGFloat systemAccessoryWidths[] = {
    [UITableViewCellAccessoryNone] = 0,
    [UITableViewCellAccessoryDisclosureIndicator] = 34,
    [UITableViewCellAccessoryDetailDisclosureButton] = 68,
    [UITableViewCellAccessoryCheckmark] = 40,
    [UITableViewCellAccessoryDetailButton] = 48
    };
    contentViewWidth -= systemAccessoryWidths[cell.accessoryType];
    }

    CGSize fittingSize = CGSizeZero;

    // If auto layout enabled, cell's contentView must have some constraints.
    BOOL autoLayoutEnabled = cell.contentView.constraints.count > 0 && !cell.fd_enforceFrameLayout;

    if (autoLayoutEnabled) {

      if (IOS_VERSION > 10.2) {
    
          [cell.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
              make.left.mas_equalTo(0).priorityLow();
              make.right.mas_equalTo(0).priorityLow();
          }];
      }
      
      NSLayoutConstraint *tempWidthConstraint =
      [NSLayoutConstraint constraintWithItem:cell.contentView
                                   attribute:NSLayoutAttributeWidth
                                   relatedBy:NSLayoutRelationEqual
                                      toItem:nil
                                   attribute:NSLayoutAttributeNotAnAttribute
                                  multiplier:1.0
                                    constant:contentViewWidth];
      [cell.contentView addConstraint:tempWidthConstraint];
      // Auto layout engine does its math
      fittingSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
      NSLog(@"fittingSize === %@",NSStringFromCGSize(fittingSize));
      [cell.contentView removeConstraint:tempWidthConstraint];
    

    } else {

      // If not using auto layout, you have to override "-sizeThatFits:" to provide a fitting size by yourself.
      // This is the same method used in iOS8 self-sizing cell's implementation.
      // Note: fitting height should not include separator view.
      SEL selector = @selector(sizeThatFits:);
      BOOL inherited = ![cell isMemberOfClass:UITableViewCell.class];
      BOOL overrided = [cell.class instanceMethodForSelector:selector] != [UITableViewCell instanceMethodForSelector:selector];
      if (inherited && !overrided) {
          NSAssert(NO, @"Customized cell must override '-sizeThatFits:' method if not using auto layout.");
      }
      fittingSize = [cell sizeThatFits:CGSizeMake(contentViewWidth, 0)];
    

    }

    // Add 1px extra space for separator line if needed, simulating default UITableViewCell.
    if (self.separatorStyle != UITableViewCellSeparatorStyleNone) {
    fittingSize.height += 1.0 / [UIScreen mainScreen].scale;
    }

    if (autoLayoutEnabled) {
    [self fd_debugLog:[NSString stringWithFormat:@"calculate using auto layout - %@", @(fittingSize.height)]];
    } else {
    [self fd_debugLog:[NSString stringWithFormat:@"calculate using frame layout - %@", @(fittingSize.height)]];
    }

    return fittingSize.height;
    }
    我这样修复了

@ChenYanTao666
Copy link

if (IOS_VERSION > 10.2) {

  [cell.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.mas_equalTo(0).priorityLow();
      make.right.mas_equalTo(0).priorityLow();
  }];

}
重点在这里

@zigang
Copy link

zigang commented Apr 8, 2017

@ChenYanTao666 preferredMaxLayoutWidth这个你是怎么设置呢?设置成0?

@yansun2006
Copy link

这么好的库,希望作者能持续更新

@leizGit
Copy link
Author

leizGit commented Apr 10, 2017

@ChenYanTao666 添加了
if (SystemVersion > 10.2){
[templateLayoutCell.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0).priorityLow();
make.right.mas_equalTo(0).priorityLow();
}];
}
之后,还是不对,看看下图,上面的间距是有问题,间距被拉大了, 下面的是正常的
wx20170410-114902 2x

只有正确设置文本内容的宽度preferredMaxLayoutWidth值,才显示正常

@JiangWenjia
Copy link

相同的问题

4 similar comments
@JiangWenjia
Copy link

相同的问题

@JiangWenjia
Copy link

相同的问题

@JiangWenjia
Copy link

相同的问题

@JiangWenjia
Copy link

相同的问题

@Daren-Wang
Copy link

相同的问题,10.3之后确实失败了

@ChenYanTao666
Copy link

@zigang 不用设置这个,就按照我写的加个在计算高度的方法里面加个约束就行了

@huangzhifei
Copy link

对于UILabel多行在10.3及以上版本失效,设置preferredMaxLayoutWidth虽然能达到效果,但是有点坑呀,如果一行只有一个UILabel还好点,对于有多个控件除非固定死了宽度,不然就动态算确实很麻烦呀。

@Zysss
Copy link

Zysss commented Apr 11, 2017

我试了一下,将templateCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;这个改为YES在10.3就没问题了,不过感觉不太靠谱

@hezhixiang
Copy link

上面的方法对没有使用NSMutableAttributedString管用,相反则出问题

@shanglina
Copy link

对于有2个或以上的多行label时 , 设置这个preferredMaxLayoutWidth 加上[cell.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(0).priorityLow();
make.right.mas_equalTo(0).priorityLow();
}]; 可以解决

@ghost
Copy link

ghost commented Apr 14, 2017

@Zysss 我也这样加了,就正常了。但还是不知道原因。。。 去看了下作者的微博,他好像离开百度了,不知道现在还维不维护了。 也不知道他的邮箱账号多少,不然就可以直接发邮件问

@21074223
Copy link

相同的问题,还是希望作者修改下库

@weekwood
Copy link
Member

Will fix in next release

@iStarEternal
Copy link

iStarEternal commented Apr 22, 2017

我在cell的布局代码中增加了

[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(0);
make.width.equalTo(self.contentView.superview.mas_width);
}];

问题得到了解决

@EasonGaoDevelop
Copy link

@iStarEternal 我添这个代码报错

@weekwood
Copy link
Member

closed by 9b18d46

@janicezhw
Copy link

@ChenYanTao666
这样设置警告是没有了,但是复杂cell情况下,会更卡顿。。 看来复杂cell还是不适合用autoLayout来自适应高度。用系统estimatedRowHeight 也会卡,而且内存占用更高

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests