从iOS 8 SDK开始,现在有identifier
property on NSLayoutConstraint。请注意,此属性自iOS 7起私人存在(仅适用于Apple),但现在已在iOS 8中公开,即使在iOS 7上运行时也允许使用该属性。
使用此属性可以轻松地为您的约束设置一个独特的简短描述来帮助调试。例如,使用:
NSLayoutConstraint *constraint = [label autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:imageView];
constraint.identifier = @"Label Left Padding";
// ...or using PureLayout v2.0+:
[[label autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:imageView] autoIdentify:@"Label Left Padding"];
// PureLayout v2.0+ also supports a block-based version to set an identifier to many constraints at once:
[UIView autoSetIdentifer:@"Constraints to position image view" forConstraints:^{
// ...a bunch of PureLayout API calls here that create constraints...
}];
然后,你会看到这个标识旁边控制台约束打印,如果有一个约束的例外。
另一个方便的调试工具:
苹果对UIView的命名UIConstraintBasedLayoutDebugging
一个非常方便的类别(在UIView.h
声明),其中包括一个方法:
- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis;
您可以调用此方法在任何视图,传递水平轴或垂直轴(因为每个轴的约束都是独立的),并获取影响沿该轴的位置和大小的所有约束的列表。请注意,Apple表示这应该只用于调试 - 绝不会发布使用此API的代码!
您可以调用'_subtreeDescription'通过增加对'UIView'临时类声明的方法 - 让编译器不会抱怨 - 然后就调用它。 – ravron 2014-09-02 13:59:24
如果可能,你能举一个例子吗?我不确定我是否遵守。 – dMurdZ 2014-09-02 14:03:21
在进一步调查中,'_subtreeDescription'只能在NSView(OS X)上使用,而不能在UIView中使用,所以这种方法不会对你有所帮助。您可能需要仔细查看日志消息以查找相关视图的内存地址。然后,尝试通过在适当的地方暂停执行来检查您的子视图的地址来找到该视图。或者,使用lldb递归地询问违规视图的超级视图。这会让你追溯它的祖先。 – ravron 2014-09-02 14:11:30