31

During a talk from WWDC 2012 (Best Practices for Mastering Auto Layout), the presenter said that you can set a UIView identifier in Xcode to aid in debugging auto layout:

Identifier for UIView

This seems like a really good idea, but in Xcode 4.5.1 for my iOS project, there is no way that I can see to set the Identity of a UIView.

How can I set the Identity of a UIView in Xcode 4.5.1? If this isn't possible in iOS projects, how can I get the same functionality?

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129
  • 1
    See here http://stackoverflow.com/questions/8295471/storyboard-doesnt-contain-a-view-controller-with-identifier –  Oct 21 '12 at 18:50
  • @Oleg this only works for ViewControllers. The OP is asking about instances of UIView as per the screenshot. – Fogmeister Jun 11 '13 at 15:00

7 Answers7

23

Setting accessibilityIdentifier on UIView does the trick. Tested on Xcode 6.4, iOS 8.4.

kean
  • 1,561
  • 14
  • 22
5

Seem like Identifier is only available on NSView in Mac OSX only. It's not available on UIView in iOS.

Find the bad constraint or constraints.

To get the constraints affecting a particular view, use constraintsAffectingLayoutForOrientation:. You can then inspect the constraints in the debugger. They are printed using the visual format notation. If your views have identifiers (see identifier (NSView)), they print out using the identifier in the description, like this:

As described in here: http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/debugging.html

Community
  • 1
  • 1
TuanCM
  • 203
  • 1
  • 4
  • 12
4

Setting in code, worked for me:

code:

imageView.accessibilityIdentifier = "profileImageView"

output:

<NSLayoutConstraint:0x17028eec0 profileImageView.width == 40   (active, names: profileImageView:0x10fd48110 )>
Wilson
  • 9,006
  • 3
  • 42
  • 46
2

Unfortunately, there doesn't seem to be any way of doing this. I have tried filling almost everything, and nothing worked. Neither restorationId, nor Accessibility traits have any effect on this. If you look at the screenshot you will see that actually he is setting a NSView, which does have the identifier property.

José Manuel Sánchez
  • 5,215
  • 2
  • 31
  • 24
0

Not quite the answer you were looking for, but what's helpful is doing something like this in the debugger :

expr [(UIButton*)0x12345 setBackgroundColor:[UIColor purpleColor]]

This helps identifying the view. Make sure to hit run on the debugger to see it take effect though.

Mustafa
  • 5,307
  • 1
  • 20
  • 19
0

Here is a handy trick you can do to aid in your AutoLayout debugging. You can add your own name property to UIView via a category, and overload its description method to include your new name. This doesn't quite give you a visible name in the AutoLayout debug info, but lets you easily po a view from its address and see its given name.

Then just assign the applicable names in your view controller:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.firstView.name = @"MyViewController.firstView";
    self.secondView.name = @"MyViewController.secondView";
}

Now when you see something like this:

<NSAutoresizingMaskLayoutConstraint:0x175086220 h=-&- v=-&- UIView:0x147533250.height == UIView:0x14760b4a0.height>

You can just po the view addresses:

po 0x147533250
MyViewController.firstView <UIView: 0x147533250>
po 0x14760b4a0
MyViewController.secondView <UIView: 0x14760b4a0>

Here's the category code:

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
devios1
  • 36,899
  • 45
  • 162
  • 260
-5

Instead of Identifier, use the Storyboard ID field. It is the same.

Luca
  • 9,259
  • 5
  • 46
  • 59