我正在以编程方式使用自动布局约束来布局我的自定义 UITableView 单元格,并且我在 tableView:heightForRowAtIndexPath:
它在 iOS6 上运行良好,在 iOS7 上也看起来正常
但是当我在 iOS7 上运行应用程序时,我在控制台中看到的消息是这样的:
中断 objc_exception_throw 以在调试器中捕获它.<UIKit/UIView.h> 中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助.2013-10-02 09:56:44.847 Vente-Exclusive [76306:a0b] 无法同时满足约束条件.以下列表中的至少一个约束可能是您不想要的.试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它.(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)("<NSLayoutConstraint:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50] (名称:'|':UITableViewCellContentView:0xd93e850)>","<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>","<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>","<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>","<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-|(名称:'|':UITableViewCellContentView:0xd93e850)>","<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>")将尝试通过打破约束来恢复<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>
而且确实在那个列表中有一个我不想要的约束:
"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
我无法将 contentView
的 translatesAutoresizingMaskIntoConstraints
属性设置为 NO => 它会弄乱整个单元格.
44 是默认单元格高度,但我在表格视图委托中定义了我的自定义高度,那么为什么单元格 contentView 有这个约束?什么可能导致这种情况?
在 iOS6 中没有发生这种情况,在 iOS6 和 iOS7 上一切看起来都很好.
我的代码比较大,这里就不贴了
在单元初始化时指定我的操作方式:
translatesAutoresizingMaskIntoConstraints
属性设置为 NOcontentView
的子视图contentView
我也很想了解为什么这种情况只发生在 iOS7 上.
我也遇到了这个问题.. 似乎 contentView 的框架在调用 layoutSubviews
之前不会更新,但是框架单元格的 较早更新,在评估约束时将 contentView 的框架设置为 {0, 0, 320, 44}
.
更详细地查看 contentView 后,似乎不再设置 autoresizingMasks.
在约束视图之前设置 autoresizingMask 可以解决此问题:
- (id)initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier{self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];如果(自己){self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;[自加载视图];[自我约束视图];}回归自我;}
Swift5、ios14:
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
I'm using auto layout constraints programmatically to layout my custom UITableView cells and I'm correctly defining the cell sizes in tableView:heightForRowAtIndexPath:
It's working just fine on iOS6 and it does look fine in iOS7 as well
BUT when I run the app on iOS7, here's the kind of message I see in the console:
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2013-10-02 09:56:44.847 Vente-Exclusive[76306:a0b] 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)
(
"<NSLayoutConstraint:0xac4c5f0 V:|-(15)-[UIImageView:0xac47f50] (Names: '|':UITableViewCellContentView:0xd93e850 )>",
"<NSLayoutConstraint:0xac43620 V:[UIImageView:0xac47f50(62)]>",
"<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>",
"<NSLayoutConstraint:0xac43680 V:[UIView:0xac4d0f0(1)]>",
"<NSLayoutConstraint:0xac436b0 V:[UIView:0xac4d0f0]-(0)-| (Names: '|':UITableViewCellContentView:0xd93e850 )>",
"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0xac43650 V:[UIImageView:0xac47f50]-(>=0)-[UIView:0xac4d0f0]>
And indeed there's one of the constraint in that list I don't want :
"<NSAutoresizingMaskLayoutConstraint:0xac6b120 h=--& v=--& V:[UITableViewCellContentView:0xd93e850(44)]>"
and I cannot set the translatesAutoresizingMaskIntoConstraints
property of the contentView
to NO => it would mess up the entire cell.
44 is the default cell height but I defined my custom heights in the table view delegate so why does the cell contentView has this constraint? What could cause this?
In iOS6 it's not happening and everything looks just fine on both iOS6 and iOS7.
My code is quite big so I won't post it here but feel free to ask for a pastebin if you need it.
To specify how I'm doing it, on cell intialization:
translatesAutoresizingMaskIntoConstraints
property to NOcontentView
of the cell contentView
I'm also deeply interested in understanding why this happens only on iOS7.
I had this problem as well.. It appears that the contentView's frame doesn't get updated until layoutSubviews
is called however the frame of the cell is updated earlier leaving the contentView's frame set to {0, 0, 320, 44}
at the time when the constraints are evaluated.
After looking at the contentView in more detail, It appears that autoresizingMasks are no longer being set.
Setting the autoresizingMask before you constrain your views can resolve this issue:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self)
{
self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self loadViews];
[self constrainViews];
}
return self;
}
Swift5, ios14:
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
这篇关于UITableViewCell中iOS7上的自动布局约束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!