I have discovered a strange behavior in my application, where a connected IBOutlet
has its connected view's frame between the calls in my view controller to viewWillAppear:
and viewDidAppear:
. Here is the relevant code in my UIViewController
subclass:
-(void)viewWillAppear:(BOOL)animated {
NSLog(@"%@", self.scrollView);
}
-(void)viewDidAppear:(BOOL)animated {
NSLog(@"%@", self.scrollView);
}
and the resulting log output:
MyApp[61880:c07] <UIScrollView: 0x1057eff0; frame = (0 0; 0 0); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x10580100>; layer = <CALayer: 0x1057f210>; contentOffset: {0, 0}>
MyApp[61880:c07] <UIScrollView: 0x1057eff0; frame = (0 44; 320 416); clipsToBounds = YES; autoresize = TM+BM; gestureRecognizers = <NSArray: 0x10580100>; layer = <CALayer: 0x1057f210>; contentOffset: {0, 0}>
Which clearly shows that the frame is changing between the two calls. I wanted to do setup with the view in the viewDidLoad
method, but if the content is not available for me to change until it is on the screen, that seems pretty useless. What could be happening?
From the documentation:
viewWillAppear:
Notifies the view controller that its view is about to be added to a view hierarchy.
viewDidAppear:
Notifies the view controller that its view was added to a view hierarchy.
As a result the frames of the subviews aren't yet set in the viewWillAppear
:
The appropriate method to modify your UI before the view is presented to the screen is:
viewDidLayoutSubviews
Notifies the view controller that its view just laid out its subviews.