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

ios10正式版不能计算出正确高度 #242

Closed
jessedog opened this issue Sep 20, 2016 · 56 comments
Closed

ios10正式版不能计算出正确高度 #242

jessedog opened this issue Sep 20, 2016 · 56 comments

Comments

@jessedog
Copy link

jessedog commented Sep 20, 2016

在iOS10上计算高度调用 fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration 方法时,获取到的contentViewWidth是1000,导致不能正确计算高度了

@renzifeng
Copy link

我也遇到问题了

@shmchen
Copy link

shmchen commented Sep 23, 2016

同上,xcode7真机调试正常,xcode8就出问题,布局全错乱了,日了狗了!纯代码 + masonry,不知道iOS10做了什么。、、、、

@beiliubei
Copy link

+1 乱了

@jessedog
Copy link
Author

cell的frame是可以正确获取到的,把contentViewWidth的获取方式改成就可以了
CGFloat contentViewWidth = CGRectGetWidth(templateLayoutCell.frame);

@bankim9507
Copy link

Facing the same issue. :/

@appjar
Copy link

appjar commented Sep 24, 2016

same issue

@wells1013
Copy link

+1

2 similar comments
@zb737472783
Copy link

+1

@Sarah-Callies
Copy link

+1

@mizhdi
Copy link

mizhdi commented Sep 26, 2016

+1 感觉是约束的问题

@wells1013
Copy link

wells1013 commented Sep 26, 2016

<NSLayoutConstraint:0x6080004888e0 UITableViewCellContentView:0x7f90e861e480.width == 0>

ios10 上 悄悄的添加了 UITableViewCellContentView.width = 0 的约束

@MagicianDL
Copy link

这几天发现了好玩的事情:我使用 storyBoard 和 xib 创建的 tableview 和 cell ,在 Xcode 7 上的时候使用 FD 没有问题,过渡到 Xcode 8 时原先在 Xcode 7 创建的东西也没有问题,可是使用 Xcode 8 创建的 cell 添加到 Xcode 7 时创建 tableview 上就出现高度计算不准的问题,然后完全使用 Xcode 8 来创建 tableView 和 cell 或是将 Xcode 7 创建的 cell 添加到 Xcode 8 创建 tableview 上,FD 也没有问题。

@xylinuxkernel
Copy link

+1

@caprice
Copy link

caprice commented Sep 26, 2016

今天刚升级就碰到了同样的问题,不知如何解决。看大牛的了

@luoqg1112
Copy link

+1

@yingql
Copy link

yingql commented Sep 26, 2016

  • 1024

@shmchen
Copy link

shmchen commented Sep 27, 2016

终于知道我的cell约束是什么问题了,果然是iOS10的薪区别

@wells1013
Copy link

@shmchen 如何解决的?

@zb737472783
Copy link

@shmchen 同问;

@imengX
Copy link

imengX commented Sep 28, 2016

将约束add到cell就行了,不要add到cell.contentView。
like:

    _truckImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
    _truckImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:_truckImageView];

    [_truckImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self).mas_offset(OMCarInfoDetailImageCellEdgeInsets);
    }];

@wells1013
Copy link

@imengX 这样的话像左滑删除按钮会有问题

@imengX
Copy link

imengX commented Sep 28, 2016

@wells1013 恩确实是
不过我发现我约束加到 cell.contentView 上也没问题
你 Xcode 版本? 我的是 Version 8.0 (8A218a)

@shmchen
Copy link

shmchen commented Sep 28, 2016

@wells1013 @zb737472783 不知道你们的情况跟我一样不;我发现如果是纯代码做cell的自适应约束的话,比方说cell的contenView底部 = 子控件底部,iOS10以下添加约束,父控件底部 = 子控件底部 or 子控件底部 = 父控件底部都可以,但是在iOS10下,我这边“必须”设置成子控件底部 = 父控件底部,不然估算高度就出不来;并且奇葩的时,连左右约束全都能给错乱掉!(并且在xib中没有什么区别,怎么做这个约束都正常!!!)

@rangang
Copy link

rangang commented Sep 28, 2016

iOS9以下没问题,iOS10高度自适应失效

@wells1013
Copy link

wells1013 commented Sep 28, 2016

@imengX 版本一样的。
@shmchen 我看到 iOS10 在 updateConstraints 方法里 默默的添加了几个约束
<NSLayoutConstraint:0x6080004888e0 UITableViewCellContentView:0x7f90e861e480.width == 0>
这些约束在ios9上都没有的。

@shmchen
Copy link

shmchen commented Sep 28, 2016

@wells1013 可能是吧,我昨天测试的时候,曾经也要自己加一个 contenView的宽等于屏幕宽的约束,不然会导致我自己添加的等屏宽的分割线长度出现问题,但是有这个contenView宽度约束在iOS10上面又再次造成布局混乱。可是我后来测试又不用添加这个约束也都正常了,很神奇!

@imengX
Copy link

imengX commented Sep 28, 2016

@shmchen 我这都没问题 是不是约束你约束设置的不完整

@shmchen
Copy link

shmchen commented Sep 28, 2016

@imengX 不太应该,在iOS10以下的版本都正常!

@gao520sun
Copy link

ios有问题

@SilenceL
Copy link

找到 UITableView+FDTemplateLayoutCell.m 中的
CGFloat contentViewWidth = CGRectGetWidth(self.frame);
改成:
CGFloat contentViewWidth = CGRectGetWidth(self.frame);
if (contentViewWidth == 0) {
contentViewWidth = CGRectGetWidth(cell.frame);
}

@zb737472783
Copy link

@SilenceL 改了,试了下,在Xcode 8运行真机iOS10 cell高度还是不正确...

@wells1013
Copy link

有什么办法可以删除UITableViewCellContentView的width,height = 0的约束条件呢

@SilenceL
Copy link

检查自己约束是否设置正确,我的就是 Xcode 8运行真机iOS10 @zb737472783

@zb737472783
Copy link

zb737472783 commented Sep 30, 2016

@SilenceL 嗯,问题就是Xcode 7 编译出来没问题,换Xcode 8 就不对了,什么都一样...非常惆怅

@SilenceL
Copy link

SilenceL commented Oct 3, 2016

但是不知道你有没想过,为什么demo不报错,我开始也以为有问题,但是demo不报错我就打消了这个问题 @zb737472783

@SilenceL
Copy link

SilenceL commented Oct 5, 2016

恩,当然是真机,真的一点问题没有!没做任何其它的改动了,自己将约束检查下! @hifall

@wells1013
Copy link

Masonry 的issues 里 有 相同的问题
SnapKit/Masonry#382

@xiao-xi
Copy link

xiao-xi commented Oct 9, 2016

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

@fifyrio
Copy link

fifyrio commented Oct 9, 2016

其实contentViewWidth一般是固定的值,所以我的做法直接定死contentViewWidth,比如contentViewWidth = [UIScreen mainScreen].bounds.size.width

@binfy
Copy link

binfy commented Oct 9, 2016

遇到同样的问题

@jacoblee88
Copy link

如果是用的xib写的cell,在使用cell的各个子控件的frame之前,调用[self layoutIfNeeded]即可!

@apascual
Copy link

Not entirely sure, but looks like on iOS10 the cell's contentView is missing some constraints. Adding some to keep the contentView edges linked to the cell's view seems to work.

I am using PureLayout and this seems to work (on awakeFromNib):
[self.contentView autoPinEdgesToSuperviewEdges];
[self.contentView layoutIfNeeded];

@binfy
Copy link

binfy commented Oct 12, 2016

那位兄弟知道这个啊
// Auto layout engine does its math
fittingHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
算这个高度的时候约束有问题,而且得到高度是900多,怎么解决呢?
NSLayoutConstraint:0x17468c990 UITableViewCellContentView:0x153d532d0.width == 375,
<NSLayoutConstraint:0x170890cc0 UITableViewCellContentView:0x153d532d0.width == 0

@hahazheng
Copy link

@yrx0910 大兄弟 你的这个问题解决了吗 ?

@hahazheng
Copy link

@monsterTesla cell宽度约束的冲突,你的这个问题解决掉了吗 ?

@binfy
Copy link

binfy commented Oct 14, 2016

@hahazheng 你好,最新的这个库是没问题的,我一直以为是这个库的事,约束正确就没问题了。我的问题出现原因是调用layoutIfNeeded。TableView有很多Cell,有个View 的约束constant ,0和120。我在点击某一项的时候,需要刷新的时候需要改变所有Cell,View的constant的值,而且要需要动画,所以我需要调layoutIfNeeded,我现在是只是当前cell constant = 120调layoutIfNeeded,其他cell的constant为0就不调了,然后问题就解决了。我的项目中很多地方用到这个库,只有一个地方有这个需求,也就这个地方调了layoutIfNeeded。不知道你是不是这个原因。你看看你是不是调layoutIfNeeded。

@hahazheng
Copy link

@yrx0910 嗯、你好、项目需求我没有调用layoutIfNeed,而且约束显示是正确的,只是在iOS 10 下会报哪个Cell宽度414(plus) 和 0约束的警告、

@binfy
Copy link

binfy commented Oct 14, 2016

可能是约束的事,我其他写的界面也挺复杂的,都没有事呢

@binfy
Copy link

binfy commented Oct 18, 2016

@hahazheng 你可以在初始化tableView,或者说实在ViewDidAppear之前设置 self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, [UIScreen mainScreen].bounds.size.width, self.tableView.frame.size.height);试试,我今天测试了可行。

@celljay86714
Copy link

我这边出现的现象是 uilabel 不自动换行,
解决办法是,原来在label在 xib中设置的 preferredMaxLayoutWidth 为0,如果改为当前在xib的宽度就正常了。我看了demo中也是设置的0,为何它能换成了。。奇怪。
第二,就是 cell初始化的时候 添加 [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }];

@hahazheng
Copy link

@yrx0910 @celljay86714 好的,我这边也没有什么问题了,这个库是没有任何问题的,还是约束的问题,我是通过自定义BOOL属性的方式,来影响约束变化,你可以调用这个方法在未来条件成立的某个时候进行更新。然后,系统会调用updateConstraints来布局。但是之后我又调用了-updateConstraintsIfNeeded 来更新约束,它是不会有强制布局的这个过程。把这个去掉发现就没有之前的Layout警告了,视图显示正常。后来也尝试了UITableView的代理方法自动缓存高度,也是可以达到预期的效果的。哈

@ArthurChi
Copy link

通过内部控件来约束好cell的宽度和高度, 但是这样会有警告, 消除警告的方式就是在约束之前, 给contentView的宽度指定为屏幕宽度就可以了.

@greedbell
Copy link

对 contentView 设置约束还是会有警告,会出现两个对宽度约束且值不一样。我的做法是用 Vuew 去把 contentView 撑起来:

[_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.contentView);
    make.right.equalTo(self.contentView);
    make.width.equalTo(self).priorityLow();
}];

这样就不会有警告

@wods
Copy link

wods commented Apr 20, 2017

我这里遇到的情况是FD dequeue出来的template cell的宽度是320,FD会手动加个屏幕宽度的constraint咋contentView上,然后不知道为何有个0的width会加在contentView上,layout时候就会提示两个constraint冲突。但也不知道这个0的width是怎么加上去的。

@weekwood
Copy link
Member

same as #291. will fix in coming release

@xiaobeifeng
Copy link

  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier中加这句话解决了哎
[self.contentView layoutIfNeeded];

ps:纯代码写的哈

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