Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController returns invalid frame?

When I start my ViewController in landscape mode (After viewDidLoad is called), I print the frame it's giving me the frame size for portrait mode instead.

Is this a bug any suggestions?

- (void)viewDidLoad {    [super viewDidLoad];     NSLog(@"%@", NSStringFromCGRect(self.view.frame));    // Result is (0, 0 , 768, 1024) } 
like image 740
aryaxt Avatar asked Mar 02 '12 20:03

aryaxt


2 Answers

Here's a clean solution.

I was experiencing the same issue and I insist on using frame throughout my app. I do not want to make an exception for the root view controller. I noticed that the root view controller's frame was displaying portrait dimensions, but all subviews had correct landscape dimensions.

Since it is only the root view controller that behaves differently, we can set a standard, blank view controller as the root view controller and add our custom view controller to that. (Code below.)

You can then use frame as you intend to, in your custom view controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];     // Override point for customization after application launch.      self.window.rootViewController = [[UIViewController alloc] init];      [self.window makeKeyAndVisible];      self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];     [self.window.rootViewController addChildViewController:self.viewController];     [self.window.rootViewController.view addSubview:self.viewController.view];      return YES; } 
like image 44
Timo Avatar answered Oct 02 '22 14:10

Timo


There are a couple of things that you don't understand.

First, the system sends you viewDidLoad immediately after loading your nib. It hasn't even added the view to the view hierarchy yet. So it hasn't resized your view based on the device's rotation either.

Second, a view's frame is in its superview's coordinate space. If this is your root view, its superview will be the UIWindow (once the system actually adds your view to the view hierarchy). The UIWindow handles rotation by setting the transform of its subview. This mean that the view's frame will not necessarily be what you expect.

Here's the view hierarchy in portrait orientation:

(lldb) po [[UIApp keyWindow] recursiveDescription] (id) $1 = 0x09532dc0 <UIWindow: 0x9632900; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x96329f0>>    | <UIView: 0x9634ee0; frame = (0 20; 768 1004); autoresize = W+H; layer = <CALayer: 0x9633b50>> 

and here's the view hierarchy in landscape-left orientation:

(lldb) po [[UIApp keyWindow] recursiveDescription] (id) $2 = 0x09635e70 <UIWindow: 0x9632900; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x96329f0>>    | <UIView: 0x9634ee0; frame = (20 0; 748 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x9633b50>> 

Notice that in landscape orientation, the frame size is 748 x 1024, not 1024 x 748.

What you probably want to look at, if this is your root view, is the view's bounds:

(lldb) p (CGRect)[0x9634ee0 bounds] (CGRect) $3 = {   (CGPoint) origin = {     (CGFloat) x = 0     (CGFloat) y = 0   }   (CGSize) size = {     (CGFloat) width = 1024     (CGFloat) height = 748   } } 

Presumably you want to know when the view's transform, frame, and bounds get updated. If the interface is in a landscape orientation when your view controller loads its view, you will receive messages in this order:

{{0, 0}, {768, 1004}} viewDidLoad {{0, 0}, {768, 1004}} shouldAutorotateToInterfaceOrientation: {{0, 0}, {768, 1004}} shouldAutorotateToInterfaceOrientation: {{0, 0}, {768, 1004}} viewWillAppear: {{0, 0}, {768, 1004}} shouldAutorotateToInterfaceOrientation: {{0, 0}, {768, 1004}} shouldAutorotateToInterfaceOrientation: {{0, 0}, {768, 1004}} willRotateToInterfaceOrientation:duration: {{0, 0}, {1024, 748}} viewWillLayoutSubviews {{0, 0}, {1024, 748}} layoutSubviews {{0, 0}, {1024, 748}} viewDidLayoutSubviews {{0, 0}, {1024, 748}} willAnimateRotationToInterfaceOrientation:duration: {{0, 0}, {1024, 748}} shouldAutorotateToInterfaceOrientation: {{0, 0}, {1024, 748}} viewDidAppear: 

You can see that your view's bounds change after you receive willRotateToInterfaceOrientation:duration: and before you receive viewWillLayoutSubviews.

The viewWillLayoutSubviews and viewDidLayoutSubviews methods are new to iOS 5.0.

The layoutSubviews message is sent to the view, not the view controller, so you will need to create a custom UIView subclass if you want to use it.

like image 87
rob mayoff Avatar answered Oct 02 '22 14:10

rob mayoff