Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Predicting UI element frames in -shouldAutorotateToInterfaceOrientation:

Is it possible to query (programmatically, at runtime) what the height/thickness of a navigation bar and/or toolbar will be post-rotation?

That is, when building a frame for a specific subview, I want to set its frame relative to the available space between the navigation bar and the toolbar. To make it smooth, it's best to do this in shouldAutorotateToInterfaceOrientation: rather than didRotateFromInterfaceOrientation:, but values such as self.navigationController.toolbar.frame.size.height are different between orientations and devices.

I'd like to calculate my new subview frame against what the toolbar's thickness will be, and I'd like to do it without hardcoding values such as 32pt, 44pt, etc.

Any thoughts?

like image 692
MikeyWard Avatar asked May 05 '11 09:05

MikeyWard


2 Answers

You should do all your frame resizing in willAnimateRotationToInterfaceOrientation:duration:

AFAIK there is no way to predict the size of your UIViews until you reach didRotateFromInterfaceOrientation:

One way could be to do all the frames resizing in your code, including the toolbar. That way you have full control over the size of your UI elements.

like image 60
Valerio Santinelli Avatar answered Nov 07 '22 07:11

Valerio Santinelli


You can change your views frame in willAnimateRotationToInterfaceOrientation:duration: method.

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];

    NSLog(@"%@", NSStringFromCGRect(self.navigationController.navigationBar.frame));
}

For more information take a look at the documentation provided by Apple.

like image 1
Evgeny Shurakov Avatar answered Nov 07 '22 07:11

Evgeny Shurakov