ios – 具有AutoLayout的UITableViewCell – contentView宽度约束失败

ios – 具有AutoLayout的UITableViewCell – contentView宽度约束失败,第1张

概述我在iOS 8中有一个动态大小的tableViewCell问题.虽然它在视觉上看起来不错,我得到的日志输出与AutoLayout错误.我把它简化为这个简单的例子. 我在我的单元格中添加了一个UILabel: self.titleLabel = [[UILabel alloc] init];self.titleLabel.translatesAutoresizingMaskIntoConstrai 我在iOS 8中有一个动态大小的tableVIEwCell问题.虽然它在视觉上看起来不错,我得到的日志输出与autoLayout错误.我把它简化为这个简单的例子.

我在我的单元格中添加了一个UILabel:

self.TitleLabel = [[UILabel alloc] init];self.TitleLabel.translatesautoresizingMaskIntoConstraints = NO;self.TitleLabel.numberOflines = 0;self.TitleLabel.lineBreakMode = NSlineBreakByWorDWrapPing;[self.contentVIEw addSubvIEw:self.TitleLabel];

我在代码中创建自动布局约束,在updateConstraints中使用Masonry:

[self.TitleLabel setContentCompressionResistancePriority:UILayoutPriorityrequired forAxis:UILayoutConstraintAxisvertical];[self.TitleLabel updateConstraints:^(MASConstraintMaker *make) {    make.leading.equalTo(self.contentVIEw.leading).with.offset(padding.left);    make.trailing.equalTo(self.contentVIEw.trailing).with.offset(-padding.right);    make.top.equalTo(self.contentVIEw.top).with.offset(padding.top);    make.bottom.equalTo(self.contentVIEw.bottom).with.offset(-padding.bottom / 2);}];

(我可以用make.edges一步一步,但问题是一样的)

这最初看起来很好.然后,当我对tablevIEw进行任何修改并调用[tableVIEw endUpdates](大概是触发updateContraints)时,我在控制台日志中得到以下内容:

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. (Note: If you're seeing NSautoresizingMaskLayoutConstraints that you don't understand,refer to the documentation for the UIVIEw property translatesautoresizingMaskIntoConstraints) (    "<MASLayoutConstraint:0x7ff4842551e0 UILabel:self.TitleLabel.leading == UItableVIEwCellContentVIEw:self.contentVIEw.leading + 12>","<MASLayoutConstraint:0x7ff484255240 UILabel:self.TitleLabel.trailing == UItableVIEwCellContentVIEw:self.contentVIEw.trailing - 12>","<NSLayoutConstraint:0x7ff484256df0 UItableVIEwCellContentVIEw:self.contentVIEw.wIDth ==>")Will attempt to recover by breaking constraint <MASLayoutConstraint:0x7ff484255240 UILabel:self.TitleLabel.trailing == UItableVIEwCellContentVIEw:self.contentVIEw.trailing - 12>

我不明白问题在这里 – 我想要标签有填充,但为什么这与contentVIEw的整体宽度冲突?

编辑1:

如果我删除填充,我不再收到错误.还有其他一些方法可以设置吗?

解决方法 您有三个具有相同优先级的约束,这使得系统混淆了如何满足它们.
“”
“”
“”

由于您的单元格的宽度也根据您的设备宽度固定,所以建议对子视图的约束使用一点弱的优先级.

[self.TitleLabel updateConstraints:^(MASConstraintMaker *make) {    make.leading.equalTo(self.contentVIEw.leading).with.offset(padding.left).priority(999);    make.trailing.equalTo(self.contentVIEw.trailing).with.offset(-padding.right).priority(999);    make.top.equalTo(self.contentVIEw.top).with.offset(padding.top);    make.bottom.equalTo(self.contentVIEw.bottom).with.offset(-padding.bottom / 2);}];
总结

以上是内存溢出为你收集整理的ios – 具有AutoLayout的UITableViewCell – contentView宽度约束失败全部内容,希望文章能够帮你解决ios – 具有AutoLayout的UITableViewCell – contentView宽度约束失败所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1110167.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存