Interface orientation in iOS 6.0 - iphone

How to use the following methods to support interface orientation in iOS 6.0:
shouldAutorotate
supportedInterfaceOrientations
preferredInterfaceOrientationForPresentation
As "shouldAutorotateToInterfaceOrientation" is deprecated in iOS 6.0.
Please provide code snippets to support your answers.
Thanks.

Deprecated method in iOS 5:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Replacement in iOS 6 and equivalent of this deprecated iOS 5 method above:
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
Hope this helps.
[edit #1: Added my UIViewController which successfully starts in Portrait mode in XCode 4.5 on iPhone 6.0 Simulator]
#import "FirstViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
[#edit 2: Sample code from landscape only application which supports iOS 5 and iOS 6]
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}

By the way, your settings on your Xcode project settings now take precedence.
Make sure that you set the "Supported interface orientations" array properly in your project's settings.
That was the issue for me. Removed the undesired ones and my app worked like it did when I compiled with Xcode 4.4.1

My app has an instance of a custom UINavigationController subclass, that presents several view controllers, all in portrait only, except when playing a video, in which case I want to additionally allow both landscape orientations.
Based on #uerceg 's answer, this is my code.
First, I enabled Portrait, Landscape Left and Landscape right in Xcode -> Target -> Summary.
In the UINavigationController subclass's implementation, I #import'ed <MediaPlayer/MediaPlayer.h>.
Then I implemented these methods:
// Autorotation (iOS <= 5.x)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {
// Playing Video: Anything but 'Portrait (Upside down)' is OK
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else{
// NOT Playing Video: Only 'Portrait' is OK
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {
// Playing Video, additionally allow both landscape orientations:
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
orientations |= UIInterfaceOrientationMaskLandscapeRight;
}
return orientations;
}

NicolasMiari's code worked for me. A little different spin I had a UITabBarController that presented UINavigationControllers and I was using StoryBoards. The UITabBarController's subclass's implementation is exactly the same and be patient with the Class selection for the Tab Bar Controller in Story Boards. It isn't immediately available even after building.

https://devforums.apple.com/thread/165384?tstart=0
https://devforums.apple.com/thread/166544?tstart=0
There are a number of examples and suggestions in the above threads relating to supporting interface orientation changes on iOS6, the two threads related to issues with game centre views but should be enough to get you started.
You should also check the iOS6 release notes under UIKit, unfortunately I can't give you a direct link since I'm new.
Avoiding posting code here due to NDA
Hope that helps

Related

how to enable rotation only on video

I've been looking for the past days a solution to enable rotation ONLY when MPMoviePlayerViewController is triggered, without any luck.
I tried to enable the app to all rotations, and disabling it at each view, but it didn't work either.
does any one have an idea?
In target settings, enable all orientations
(optional, UIViewController default is portrait only) In all the other view controllers, do the following:
// Deprecated in iOS 6, but still needed for earlier versions of iOS:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
// For iOS 6 and up
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
In the one view controller that should rotate, do the following:
// Deprecated in iOS 6, but still needed for earlier versions of iOS:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
// For iOS 6 and up
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
This is assuming the rotating view controller will not be inside the same container view controller (such as UINavigationController or UITabBarController) as the non-rotating view controllers.
Create a new ViewController for the player Full screen and add:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
to your code.
Make sure you present the ViewController.
(supporting iOS 6 and above)

ios 6 orientation methods

the orientation methods have changed in iOS 6.
my whole app in portrait mode got to many view controllers (not tab bar view controllers) i just want to rotate one of my view controller to landscape mode (it actually displays a webView) when i rotate the device.the below method was working in xcode 4.4 but, it's not in Xcode.4.5
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight );
the above method won't work in xcode 4.5 for this reason i have changed the below method but even though its not working....plz any suggestions thanks.
- (BOOL) shouldAutorotate{
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait];
return self.modalViewController.shouldAutorotate;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
Do you use tab bar view controller? If you use it, then all view controllers in all tabs should be able to rotate even if you only want to rotate only one.
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
This should work fine in iOS6.
If you use UINavigationViewController, then its methods would be called. There is another solution.
// App delegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
And then in view controllers
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

iOS 6 Autorotation for Only SingleViewController in Multiple ViewController App

Is there any way to provide Autorotation to a SingleViewController as in our iPhone App have multiple ViewControllers and need to do autorotate for only One.
Yes you can use for your VC(not rotating)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return NO;
}
// New Autorotation support for iOS 6.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
{
Return NO;
}
Also see Apple Doc
It will be helpful.

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.

shouldAutorotateToInterfaceOrientation but nothing happens

our app got rejected, becouse the app does not rotate in upside down orientation.
so we have an tabbar App, adding this code to all tabs...
shouldAutorotateToInterfaceOrientation
makes no sense, add this code to a Appdelegate doesn't helps, what we do wrong?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
UITabbarcontroller is a subclass of UIViewcontroller. To solve you problem just subclass or add a category for you UITabbarcontroller implementing:
#interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
#end
#implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
If you want to make the tabbar only rotate to portrait and upside down just use th following code instead
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Make sure that each UIViewController implements
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}