这是一个方便的技巧,可以帮助您进行 AutoLayout 调试。您可以通过类别将自己的name 属性添加到UIView,并重载其description 方法以包含新的name。这并不能在 AutoLayout 调试信息中为您提供一个可见的名称,但可以让您轻松地 po 从其地址查看视图并查看其给定名称。
然后只需在您的视图控制器中分配适用的名称:
- (void)viewDidLoad {
[super viewDidLoad];
self.firstView.name = @"MyViewController.firstView";
self.secondView.name = @"MyViewController.secondView";
}
现在当你看到这样的东西时:
<NSAutoresizingMaskLayoutConstraint:0x175086220 h=-&- v=-&- UIView:0x147533250.height == UIView:0x14760b4a0.height>
你可以po查看地址:
po 0x147533250
MyViewController.firstView <UIView: 0x147533250>
po 0x14760b4a0
MyViewController.secondView <UIView: 0x14760b4a0>
这是分类代码:
UIView+Name.h
#import <UIKit/UIKit.h>
@interface UIView (Name)
@property (strong, nonatomic) NSString *name;
- (NSString *)description;
@end
UIView+Name.m
#import "UIView+Name.h"
#import <objc/runtime.h>
@implementation UIView (Name)
- (NSString *)name {
return objc_getAssociatedObject(self, @selector(name));
}
- (void)setName:(NSString *)name {
objc_setAssociatedObject(self, @selector(name), name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@ %@", self.name, [super description]];
}
@end