ios autolayout debug调试技巧
这篇文章并没有具体介绍自动布局的一些基本概念,主要讲解了一些高级的调试技巧。
这篇文章不是用来介绍Auto
Layout的。如果你还没用过它,那还是先去WWDC 2012看看基础教程吧(1,2,3)。
- (lldb) expr ((UIView *)0x7731880).backgroundColor = [UIColor purpleColor]
- (lldb) expr [(UIView *)0x7731880 setBackgroundColor:[UIColor purpleColor]]
- @implementation NSLayoutConstraint (AutoLayoutDebugging)
- #ifdef DEBUG
- - (NSString *)description
- {
- NSString *description = super.description;
- NSString *asciiArtDescription = self.asciiArtDescription;
- return [description stringByAppendingFormat:@" %@ (%@, %@)", asciiArtDescription, [self.firstItem tag], [self.secondItem tag]];
- }
- #endif
- @end
- @interface UIView (AutoLayoutDebugging)
- - (void)setAbc_NameTag:(NSString *)nameTag;
- - (NSString *)abc_nameTag;
- @end
- @implementation UIView (AutoLayoutDebugging)
- - (void)setAbc_NameTag:(NSString *)nameTag
- {
- objc_setAssociatedObject(self, "abc_nameTag", nameTag, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- }
- - (NSString *)abc_nameTag
- {
- return objc_getAssociatedObject(self, "abc_nameTag");
- }
- @end
- @implementation NSLayoutConstraint (AutoLayoutDebugging)
- #ifdef DEBUG
- - (NSString *)description
- {
- NSString *description = super.description;
- NSString *asciiArtDescription = self.asciiArtDescription;
- return [description stringByAppendingFormat:@" %@ (%@, %@)", asciiArtDescription, [self.firstItem abc_nameTag], [self.secondItem abc_nameTag]];
- }
- #endif
- @end
- static void AddTracebackToConstraints(NSArray *constraints)
- {
- NSArray *a = [NSThread callStackSymbols];
- NSString *symbol = nil;
- if (2 < [a count])
- {
- NSString *line = a[2];
- // Format is
- // 1 2 3 4 5
- // 012345678901234567890123456789012345678901234567890123456789
- // 8 MyCoolApp 0x0000000100029809 -[MyViewController loadView] + 99 //
- // Don‘t add if this wasn‘t called from "MyCoolApp":
- if (59 <= [line length])
- {
- line = [line substringFromIndex:4];
- if ([line hasPrefix:@"My"]) {
- symbol = [line substringFromIndex:59 - 4];
- }
- }
- }
- for (NSLayoutConstraint *c in constraints) {
- if (symbol != nil) {
- objc_setAssociatedObject(c, &ObjcioLayoutConstraintDebuggingShort, symbol, OBJC_ASSOCIATION_COPY_NONATOMIC);
- }
- objc_setAssociatedObject(c, &ObjcioLayoutConstraintDebuggingCallStackSymbols, a, OBJC_ASSOCIATION_COPY_NONATOMIC);
- }
- } @end
- - (NSString *)objcioOverride_description {
- // call through to the original, really
- NSString *description = [self objcioOverride_description];
- NSString *objcioTag = objc_getAssociatedObject(self, &ObjcioLayoutConstraintDebuggingShort);
- if (objcioTag == nil) {
- return description;
- }
- return [description stringByAppendingFormat:@" %@", objcioTag];
- }
- @implementation UIView (AutoLayoutDebugging)
- - (void)printAutoLayoutTrace {
- #ifdef DEBUG
- NSLog(@"%@", [self performSelector:@selector(_autolayoutTrace)]);
- #endif
- }
- @end
- @implementation UIView (AutoLayoutDebugging)
- - (void)exerciseAmiguityInLayoutRepeatedly:(BOOL)recursive {
- #ifdef DEBUG
- if (self.hasAmbiguousLayout) {
- [NSTimer scheduledTimerWithTimeInterval:.5
- target:self
- selector:@selector(exerciseAmbiguityInLayout)
- userInfo:nil
- repeats:YES];
- }
- if (recursive) {
- for (UIView *subview in self.subviews) {
- [subview exerciseAmbiguityInLayoutRepeatedly:YES];
- }
- }
- #endif
- } @end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。