iOS 6 auto rotation issue - supportedInterfaceOrientations return value not respected - iphone

I've got an app where I have a UINavigationController subclass as my rootViewController. I've got a UITableViewController that lets the user edit some settings, it should always be in portrait mode. My app also needs to support all other orientations after I push a MoviePlayer component onto the navigation controller.
The UITableViewController subclass has this implementation of supportedInterfaceOrientations:
- (NSUInteger)supportedInterfaceOrientations {
LLog();
return UIInterfaceOrientationMaskPortrait;
}
The logging command tells me that this gets actually called.
The problem is that the return value is not respected, i.e. the screen turns to landscape orientation when I turn the device.
What can I do to make the settings view always show in portrait but allow orientation changes for the video viewer?
More information: my UINavigationController subclass doesn't override shouldAutorotate or supportedInterfaceOrientations. I haven't implemented
- (NSUInteger)application:(UIApplication *)application
supportedInterfaceOrientationsForWindow:(UIWindow *)window
method in my AppDelegate and I have enabled all orientations in the target summary.

I had issue that some ViewControllers in the navigation stack support all the orientations, some only portrait, but UINavigation controller was returning all app supported orientations, this little hack helped me.
#implementation UINavigationController (iOS6OrientationFix)
-(NSUInteger) supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
#end

You also need to add:
- (BOOL)shouldAutorotate {
return NO;
}
and set the supported rotations for the root view controller in the app plist file to only portrait.

Category for UINavigationController not working for me. I don't know why. I solve my problem with such category of UIViewController:
#implementation UIViewController (Orientation)
- (BOOL) shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if ([self isKindOfClass:[PlayerViewController class]])
{
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
orientations |= UIInterfaceOrientationMaskLandscapeRight;
}
return orientations;
}
#end

Related

How to get different autorotation function in different UIViewController in iOS 6 [duplicate]

ADDED:
You can access this project on github
ios6rotations
Sorry guys for asking the question about screen rotation in iOS 6 but this is really a pain in the ass..and I still can't understand it completely - for some reason it behaves differently under certain circumstances.
I have the following simple hierarchy of views in my test app:
What I'm trying to achieve is - to keep blue controller in landscape only and red one is only in portrait.
I have a subclass of UINavigationController with such code inside:
#implementation CustomNavController
- (BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
In my blue controller I implemented this:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
And in red controller this:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Now I have the following behavior:
App started in landscape (OK)
When I press the button my red controller pushed in landscape too (this is not ok because it must be shown in Portrait)
It successfully rotates to portrait but not backward to landscape
If I leave the red controller in Portrait mode my blue controller (which is restricted to landscape) shows in Portrait mode.
P.S.
All my rotation methods(posted above) are getting called normally.(by the way why do these methods getting called so many times per screen transition - 5-6 times)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation does not getting called with pushing
All(except portraitUpsideDown) orientations are included in plist.
The question is - how to force rotation to supported orientation in each controller?
I suggest you to post here (as answers) any 100% working code to handle rotations in ios6 (for example if you have some for iPad with SplitController) - I'll keep this question in favorites to have all in one place when I need to handle some specific situations. Cheers!
ADDED:
Please do not post this as answer from landscape to portrait I hope that there'
s more elegant way to do it.
Using -[UIDevice setOrientation:] is a private API, and will get your application rejected. See this question.
What you ask is not possible using public API and is also not recommended from HIG standpoint. What is supported and you should implement, is modal presentation of the different view controllers with different supported interface orientation. This is why the default implementation of UINavigationController is to always rotate; it assumes all view controllers have the same supported interface orientations.
Take for example video playback on iPhone. Open the video apps (that comes with iOS). The root view controller only supports portrait orientation. However, start a video, and a modal view controller pops up which only supports landscape interface orientations. This seems exactly the behavior you wish to achieve.
This is why preferredInterfaceOrientationForPresentation is not called. preferredInterfaceOrientationForPresentation only gets called when using presentViewController:animated:.
A small gotcha, if you require a navigation bar in each stage of your scene, you will need to enclose each modal view controller with a navigation controller. You can then pass the required data in prepareForSegue: by accessing topViewController of the navigation controller object in the segue.
Here is an example project which behaves correctly according to your requirements (or at least will give you ideas how to implement):
http://www.mediafire.com/?zw3qesn8w4v66hy
My two cents worth.
You can present an empty transparent modal view quickly then dismiss it, maybe on ViewDidLoad: or viewWillAppear: on your ViewController and ViewControllerSecond class as a quick workaround.
Also, in storyboard, you can set ViewController class orientation to landscape visually.
use this line for programmatically change orientation... work 100%
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
and also when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file.. at the top.
#interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
and after that in bellow method just write this code if required..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return your supported orientations
if (currentMainView==blueOne) {
return toInterfaceOrientation== UIInterfaceOrientationPortrait;
}
}
I have a similar situation in one of my apps (although do note that I am not using UINavigationController).
Change the shouldAutorotate methods in both of your viewControllers:
//in blue (landscape only)
-(BOOL)shouldAutorotate{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
return YES;
}
else {
return NO;
}
}
//in red (portrait only)
-(BOOL)shouldAutorotate{
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
//note that UIInterfaceOrientationIsPortrait(self.interfaceOrientation) will return yes for UIInterfaceOrientationPortraitUpsideDown
return YES;
}
else {
return NO;
}
}
Keep the supportedInterfaceOrientations methods the same.
#pragma mark- Orientation Delegate Method:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{ Orientation = [UIApplication sharedApplication].statusBarOrientation;
if (Orientation == UIInterfaceOrientationLandscapeLeft || Orientation == UIInterfaceOrientationLandscapeRight)
{
// self.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width,1200);
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(768, 2150)];
}else if (Orientation == UIInterfaceOrientationPortrait || Orientation == UIInterfaceOrientationPortraitUpsideDown)
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(768, 1750)];
}
}
In order to use navigation with orientation together, you should take a bunch of viewcontrollers like an array.
After that checkout following methods,
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
this changes in your methods will help you a lot.
Enjoy Programming!

Landscape only UIViewController in UINavigationController

I have UITableViewController in which on one tab is UINavigationViewController. UINavigationController root view controller is UITableViewController, and when clicked on cell, UIViewController appears which has to be locked in Landscape.
I want every Controller to be locked in Portrait, except the mentioned UIViewController that must be locked in Portrait.
I have tried the following:
CustomTabBarController.m:
#import "CustomTabBarController.h"
#implementation CustomTabBarController
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return [self.selectedViewController supportedInterfaceOrientations];
}
#end
CustomNavigationController.h:
#import "CustomNavigationController.h"
#implementation CustomNavigationController
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return YES;
}
#end
And in UIViewController that must be locked in to Landscape, I have put:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotate
{
return YES;
}
But it doesn't work, I can rotate it to Landscape and it will stay locked in Landscape, but I want it to appear automatically in Landscape.
Any suggestions?
I had a big problem in the past with UITabBarController not respecting my supported interface orientations of displayed view controllers.
I solved the problem by sub-classing UITabBarController and capturing whenever an item was selected. I'd then call down to the view controller myself, ask it what the supported orientations are and force a rotation myself if needed. I would also call down to the selected view controller on rotations to set/change my supported orientations.
I implemented the UITabBarDelegate and used didSelectItem to capture tab switches. I'm not sure if there is a better way to do it now.
Try to override method
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
Try with the method to block some orientations for a particular window:
– application:supportedInterfaceOrientationsForWindow:

How to rotate a single View Controller in IOS6

How to rotate a Single ViewController in iPad in ios6 . I have used the delegate in
appdelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
and it makes all viewcontroller Portrait and I want a single viewcontroller in between only in landscape mode.
in graphViewController.m
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
but i am unable to get the correct result, Please response asap. Thanks.
See this link iOS6 Release Notes
Under UIKit -> Autorotation is changing in iOS 6
"More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController)
do not consult their children to determine whether they should
autorotate."
shouldAutorotate is never called (in iOS6) in any UIViewController which is inside another controller (for example a UINavigationController).
You should use the shouldAutorotate method of the UINavigationController (if you use one) to determine which child is display and force a given orientation
used this method in the viewController you want to rotate not in app delegate.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
because in below method you are rotating window so thats why it change for whole application.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}

I want to restrict some of the view controller to landscape in ios6

I'm trying to restrict one view controller which on top of UINavigationController. To do that i've created a UINavigationController subclass and implemented 2 methods
- (BOOL)shouldAutorotate {
return [[self.viewControllers lastObject] shouldAutorotate];}
- (NSUInteger)supportedInterfaceOrientations {
return [[self.viewControllers lastObject] supportedInterfaceOrientations];}
I want the first viewcontroller on top of UINavigationController(which is Root View Controller) should be in portrait mode and the next view controller which i'm pushing from the root view controller should be Landscape mode(ONLY).
So i'm overriding those two methods in both view controllers.
In the root view controller
- (BOOL)shouldAutorotate {
return NO;}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;}
In the next view controller
- (BOOL)shouldAutorotate {
return YES;}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;}
Its working fine but not completely. For the first time when I push the view controller its showing in portrait mode(Not restricting to landscape as I expected) and once I rotate the device/simulator and its working fine and restricting to landscape only.
Can anyone help in this?
Try this out.
Call this one in the viewWillAppear will explicitly tell the device to jump to the portrait orientation.
[[UIDevice currentDevice] performSelector:NSSelectorFromString(#"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
I don't think this is the right solution. But if you got no other options, you can use this.
Happy Coding :)
U present new controller :
SecondViewController *objSecondViewController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController presentViewController:objSecondViewController animated:NO completion:^{}];
In new controller :
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
This worked for me. Just try using this :
1) YourApp-Info.plist
Add one more array for Supported interface orientations
Add your required orientation to this dictionary
Refer below screenshot :
2) Project Target
Select the required orientation from Supported Interface Orientations
Refer below screenshot :
UIViewController have the following function. You can implement this in your view controller where you want to restict portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
if(interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
else
return NO;
}

Make a UiViewController stay in portrait mode iOS6 VS iOS5

I'm building an app for iOS5 and iOS6. I have this UIViewController inside a UINavigationController and I want it to stay in portrait mode.
The code works for iOS5, but not in iOS6.
// iOS5 rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
else
return NO;
}
// iOS6 rotation
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
What's the problem here??? On SO I've found lot's of similar questions, but generally the answers are not working for me.
EDIT:
Maybe I was not precise, but I need a SINGLE view controller (the homepage of my app) to stay in portrait mode and not all the app.
First of all, A lot depends on with which controller is your UIViewController embedded in.
Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.
subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) needs to be set it as self.window.rootViewController.
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.
MOREOVER
Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
For UIViewControllers, which require LANDSCAPE too, change masking to All.
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}
Now, if you want to do some changes when Orientation changes, then use this function.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
VERY IMPORTANT
in AppDelegate, write this. THIS IS VERY IMP.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}
If you want to provide only Portrait mode for all your viewcontrollers, then apply the portait mask. i.e UIInterfaceOrientationMaskPortrait
Otherwise, if you want that some UIViewControllers stay in Portrait while others support all orientations, then apply an ALL Mask. i.e UIInterfaceOrientationMaskAll
Try out this :
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
UIInterfaceOrientationMask orientationMask = UIInterfaceOrientationMaskPortrait;
return orientationMask;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
This should work in order to support the portrait mode only.