forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.93k stars 2.01k forks source link

UITableViewHeaderFooterView使用UITableView-FDTemplateLayoutCell警告的问题 #346

Open piaodang1234 opened 6 years ago

piaodang1234 commented 6 years ago

hello,我的tableview中使用了大量的继承于UITableViewHeaderFooterView的header和footer,在通过- (CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id))configuration(这个方法有bug,最新的版本上还没有修复,在该方法的第二行内加上configuration(templateHeaderFooterView);)获取headerview的高度。在headerview中我将创建的UI控件添加到了self.contentView中,但是会提示许多警告,如下 2017-08-21 18:55:29.472933+0800 TableViewProject[21176:566087] [LayoutConstraints] 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. ( "<NSLayoutConstraint:0x60000009e500 MASTableViewHeaderView:0x7f88af40dcd0.width == 375>", "<NSLayoutConstraint:0x60800009e960 MASTableViewHeaderView:0x7f88af40dcd0.width == 0>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x60000009e500 MASTableViewHeaderView:0x7f88af40dcd0.width == 375>

如果我不是用self.contentview,而是使用self的话,那么Masonry会提示警告“ [LayoutConstraints] Custom views and layout for UITableViewHeaderFooterView should be contained within the content view. Offending view: <MASTableViewHeaderView: 0x7fcdabc12780; baseClass = UITableViewHeaderFooterView; frame = (0 0; 0 0); text = ''; layer = <CALayer: 0x60800002d000>>” 附件是demo,使用系统的动态高度没有问题,但是我们需要相下兼容,所以只能来这里求助啦,多谢!!! TableViewProject.zip

Josscii commented 6 years ago

这个问题我刚好也遇到了,刚刚找到解决方法,尽管我没有用这个库,但是在把一个 cell 当作 tableHeaderView 的时候也遇到的同样的警告,经过我的测试和研究发现:

在已经将从上到下的约束拉通的前提下,对于多行 label 来说,不设置他距离 contentView 右边距的约束,并且 hardcode 指定 preferredMaxLayoutWidth 的值就可以解决这个问题了。

之前我一直是将多行 label 的左右都设置好约束,可能在 layout 的过程中,系统为 contentView 的宽度增加了一个隐式为 0 的约束后,导致冲突了,我感觉你提到的 headerView 是同理的。

如果成功麻烦反馈一下,谢谢。

piaodang1234 commented 6 years ago

您好,我设置了多行label的preferredMaxLayoutWidth,一样有警告,所以我怀疑有可能是类库本身有问题

Josscii commented 6 years ago

请问是否设置了 label 到父视图右边的约束呢?如果设置了,再去掉试试?

feonixsmj commented 6 years ago

我是这样解决冲突的 [self.contentView addSubview:self.commentBaseView]; self.width = SCREEN_SIZE_WIDTH; self.contentView.width = SCREEN_SIZE_WIDTH; [self.commentBaseView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.contentView); }];

charlesYun commented 3 years ago

我也遇到了,怎么解决呢