篇幅一,介绍了为什么可以通过mas_key 定位问题。如下:
https://www.jianshu.com/p/710f742ca71a
如果一个布局很多的页面,突然出现这种问题呢?比如下面的,
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.
(
"<MASLayoutConstraint:0x1c08a0a80 UILabel:0x112a1c600.left == UIView:0x112a1b830.left + 5>",
"<MASLayoutConstraint:0x1c08a1080 UILabel:0x112a1d1e0.left == UIView:0x112a1b830.left + 5>",
"<MASLayoutConstraint:0x1c08a1140 UILabel:0x112a1d1e0.right == UILabel:0x112a1c600.left - 5>"
)
信息只有UILabel UIView,我们可以通过runtime,获取到ivar 信息,然后手动给ivar 设置mas_key 值。
代码如下:
uint32_t ivarCount;
Ivar *ivars = class_copyIvarList([self class], &ivarCount);
if(ivars)
{
for(uint32_t i=0; i<ivarCount; i++)
{
Ivar ivar = ivars[i];
id pointer = object_getIvar(self, ivar);
if ([pointer isKindOfClass:[UIView class]]) {
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
UIView *v = (UIView *)pointer;
v.mas_key = name;
}
}
free(ivars);
}
通过判断ivar 是不是UIView 的子类,然后设置mas_key 的值为ivar的name.这样子提示信息就很详细了。
可以提取一个全局的函数出来
/*
* 设置ivar 的 mas_key 值为 对应的名字比如
@interface PBTTestClass : NSObject
@property (nonatomic, strong) UIButton *payButton;
@end
PBTTestClass *c = [PBTTestClass new];
autoSetClassMas_key(c);
就设置 c.payButton.mas_key = @"_payButton";
*
*/
static void autoSetClassMas_key(NSObject *instance) {
uint32_t ivarCount;
Ivar *ivars = class_copyIvarList(instance.class, &ivarCount);
if(ivars)
{
for(uint32_t i=0; i<ivarCount; i++)
{
Ivar ivar = ivars[i];
id pointer = object_getIvar(instance, ivar);
if ([pointer isKindOfClass:[UIView class]]) {
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
UIView *v = (UIView *)pointer;
v.mas_key = name;
}
}
free(ivars);
}
}
网友评论