Last Updated: February 25, 2016
·
1.401K
· kormie

iOS 7 Header View Size Debugging

Tonight I had to alter the size of a view in iOS 7. Because the View was underneath the navigation bar (which wasn't a problem until 7) I needed to see where the iOS 6 nav bar would lie, but also hide it from view (I realize that given the static sizing of navigation bars in iOS this could be done more simply but the example is given more fore demonstration than anything).

CGRect debugFrame = self.navigationBar.frame;
UIView *debugView = [[UIView alloc] initWithFrame:debugFrame];
[debugView debugMySize];
[self.view addSubview:debugView];
self.navigationBar.hidden = YES;

debugMySize is a category method I've included which ads a 1pt, random color border around any UIView. This is the code you can add to the UIView+ViewHighlighter category to do that:

-(void) debugMySize{
  int red = rand() % 255;
  int green = rand() % 255;
  int blue = rand() % 255;
  UIColor* myColor = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];

  self.layer.borderColor = [myColor CGColor];
  self.layer.borderWidth = 1;
}

EDIT

I recently had to find a subview that was causing trouble, but didn't want to go through the hassle of adding debugMySize on every subview to figure out which one was the culprit. So, I added this code to the category:

-(void)debugSubviews{
    void (^block)(id) = ^(UIView *subview) {
        [subview debugMySize];
        [subview debugSubviews];
    };
    [(self.subviews) enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        block(obj);
    }];
}

-(void)debugMyAndAllSubviewSizes {
    [self debugMySize];
    [self debugSubviews];
}

The first method loops over every subview for the current UIView, calls debugMySize on it and then recurs. The second method just calls the first and debugMySize on the UIView itself. This way you get a nice box around every subview, the subiew's subviews and so on until none are left.

2 Responses
Add your response

iOS7 has buildin helpers to define the size of the statusbar + navigationbar height. It is a good idea to add a contentsubview to your mainview. this way you only have to take the statusbar and navbar size into account on 1 view.

In your viewcontroller use self.topLayoutGuide.length to get the amount of points that take up your statusbar and navigationbar (if there is one)
You can also use self.bottomLayoutGuide.length to get the size of the tabbar if you need it.

over 1 year ago ·

Why don't you use the debug menu on iOS Simulator instead of doing it in the code by yourself?

over 1 year ago ·