UIViewController isn't aware of rotation with a modal view being shown - iphone

I have a UIViewController, which launches a modal view controller. Both have orientation support. If i rotate with the modal view controller there, it doesn't tell the view controller behind it that rotation has happened, and so when the modal view controller is dismissed, the original view is in the right orientation, but at the original sizes (e.g. i rotate it to be 480x320, the text and other items will be the correct way up for the new orientation, but will still be arranged in their 320x480 layout). How do i let the view controller know rotation has occurred?

use NSNotification.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// send a notification here
}
In your ViewController behind, add an observer and call a method to fix the layout.
An old SO answer explaining the sytntax for NSNotification
I hope its useful

Related

Modal View prevents other views from rotating

I have this method in my MainViewController:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(#"MAIN CONTAINER WILL ANIMATE");
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
And when I rotate, the NSLog shows up, and everything is perfect. However, if I present a modalViewController from my MainViewController, and then I rotate, the NSLog no longer appears, and my MainViewController never knows that the device got rotated, so when the modalView is dismissed, the interface is not adjusted for the rotation.
Any ideas as to why a modal view can prevent the parent from receiving rotation updates? Is this typical, or must there be something wrong with my setup?
And just to make sure, I tried presenting the modalViewController as a subview via [mainViewController.view addSubview:modalView.view], and the rotation updates took effect properly. It's only when I do [mainViewController presentModalViewController:modalViewController]; that the updates don't take effect.
Surely when a view controller is being presented modally, no other view controllers receive any messages at all. That is what modal means in this context.
The obvious solution would be to check the orientation in the viewWillAppear method of your modeless view controller.
You have to check the implementation of:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
In both views.
You need to adjust your layout to the current interface orientation in viewWillAppear. Obviously, because the view is not visible, the animation functions will not be called while the modal view controller is up.

Device rotation while displaying modal view controller

I have an OpenGL app which supports dynamic device orientation. I can rotate the device to any of the 4 physical orientations and everything works as expected.
Now I've added a modal view controller so that you can get a settings view (this is non-OpenGL). The modal view controller also supports any device orientation.
However, if I present the modal view controller in orientation A (e.g. portrait) and dismiss it in orientation B (e.g. landscape-right), after it is dimissed, the OpenGL view is corrupted (the aspect ratio looks wrong). I have to orient the device to a different orientation for the OpenGL view to 'correct' itself.
When the modal view controller is dismissed, it's like the app thinks that it's already in orientation B, so it doesn't ever rotate the OpenGL view.
If I disable rotation on the modal view controller, then if I present the modal controller in orientation A, rotate the device to orientation B (the modal view controller no longer rotates) and dismiss the controller, the OpenGL view gets willRotateToInterfaceOrientation/didRotateFromInterfaceOrientation and correctly orients to orientation B.
Is there any way to preserve the dynamic orientation of the modal controller, yet have the OpenGL view also properly rotate after the modal controller is dismissed?
Thank you.
I had a similar issue with a custom action sheet, and decided that the simplest answer would be to temporarily disallow rotation while the view is presented. You can see an example of this in the Apple Notes app, whenever an action sheet is presented the orientation becomes locked.
I know you've already tried this and it works, I just think you should consider that as a valid solution.
I'm not sure how to solve it otherwise, you would probably need to store any changes in orientation in your modal view controller and pass them back to the OpenGL view controller, either at the same time or once at the point of dismissing the settings view, then manually re-render.

Rotate single screen in Navigation Controller and TabBar

I have a Navigation Controller and TabBar. I would like to know how the iPod app handles allowing only the play screen to rotate. I have tried to replicate this on iOS 4.2, and if that one screen rotates to landscape and you navigate back, the old screen is too. This is undesired... is there anyway to make the 2nd screen in rotate while not causing the rest of the screens or any of the TabBars view controllers elsewhere do so?
Thanks.
Make sure your rotatable view returns YES in shouldAutorotateToInterfaceOrientation: method of UIViewController.
Make sure your non rotatable view returns NO in shouldAutorotateToInterfaceOrientation: method of UIViewController.
Subclass UITabBarController, override shouldAutorotateToInterfaceOrientation: and handle rotation here based on current UIViewController's shouldAutorotateToInterfaceOrientation: return value. You have to also check if current view controller is UINavigationController or not and if yes, you have to get current view controller from UINavigationController too.
It's not recommended to do this (I mean subclassing of UITabBarController), but UITabBarController forbids rotation if not all UIViewControllers do allow rotation.
You need to state that in the 2nd screen the device orientation is only the desired screen orientation. So for every controller you create you need to implement shouldautorotatetointerfaceorientation:.

UIView subview doesn't change orientation

I have a view controller which manages a view.
I'm adding the my view controller subclass as a subview of the window swapping out another view.
I'm running landscape mode on an iPad.
The view apparently doesn't know that its in landscape mode. Its frame is confused.
Is there something I can/should do to tell it that its in landscape, and/or that the orientation has changed. How does this normally happen. Why isn't it happening?
I used to have my view controller within a UITabBarController and it worked fine there.
Override:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
Your ViewController is not getting rotation events because you have not presented the viewController but have added the viewController's view in the view hierarchy.
Your Tab bar controller previously used to take the responsibility to forward the rotation events to the view controller which it manages, that was how it used to work.
I would though suggest that swapping the view out of window is a bad idea. Instead you should have a main viewController which accepts the rotation events and then swap the view within this viewController based on the current orientation. Consider re-desiging before you code further.
My problem was that my storyboard was overriding my existing custom coded app delegate. After I deleted the story board file, and custom generated view controller code, it worked for me.

Landscape UIView in a UITabBarController

I have a UITabbarController with (so far) two navigation controller items. I can get the application to rotate by adding the shouldAutorotateToInterfaceOrientation to each class... but thats not exactly what I want.
What I want to do is to add a button in the UINavigationBar in one of the classes. When this button is pressed I want it to load another view into landscape mode. This view should not show any navigationbar or tabbar controller.
How can I get this to work?
Best regards,
Paul Peelen
You can try to use approach similar to Apple's AlternateViews sample.
Basically you should:
Create your landscape view with appropriate size (480x300 for landscape if standard statusbar is visible)
In your button handler push your landscape calling -pushModalViewController on your current view controller
Apply necessary affine transformation to your view to be displayed correctly in landscape.
Use presentModalViewController:animated: and implement the modal view controller's shouldAutorotateToInterfaceOrientation: accordingly.