Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when a UIView has changed size?

I have a UIViewController that is initialised with a correct frame, however somewhere in my code the frame gets mangled and I'm having difficulty finding out where.

In situations like this it is usually handy to watch a variable in the debugger, however I have no way of accessing the controller->view->frame property in my variable view, since it isn't a variable, it's a property (surprisingly enough)

Drilling into the UIView in the variables display shows a few things but nothing I can relate to the frame, I thought perhaps that would be in layer but it isn't.

Is there any way to watch for changes in a private API? I guess not, since the variables are essentially 'hidden' and so you can't specify exactly what to watch.

Alternatively, what other approach could I use? I already tried subclassing UIView, setting my UIViewController's view to point to this subclass and breaking on the setFrame method but it didn't seem to work.

EDIT: the subclassing UIView method DID work, I just had to set the view to point to my test subclass in viewDidLoad and not the init method. Leaving this question open as I'm not sure if this is the best way of approaching this kind of problem...

like image 558
Sam Avatar asked Jan 27 '10 13:01

Sam


2 Answers

Subclass your the view you want to track and rewrite the setFrame method:

@implementation MyTableView

- (void)setFrame:(CGRect)frame;
{
    NSLog(@"%@", frame);
    [super setFrame:frame];
}

@end

Then use the debugger to add a breakpoint to it and check when it gets called. Eventually, you'll see when the frame gets changed and where does the change comes from.

like image 95
Marc M Avatar answered Sep 21 '22 21:09

Marc M


I discovered this can be done using key value observers.

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueObserving/KeyValueObserving.html

like image 37
Sam Avatar answered Sep 21 '22 21:09

Sam