Supported Interface Orientation and shouldAutorotateToInterfaceOrientation - iphone

In my project settings i made Supported Interface Orientation as both landscape.
Should i still implement below in each view controller?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

iOS < 6.x
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation // Deprecated in iOS 6.x
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
iOS > 6.x
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
hope this will help.

ya its better to implement and it would be better if you implement it in info.plist the orientation to landscape
for ios>6
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}

Related

Autorotation not working ios6.1

Autorotation is not working in ios6.1. But following code is working in other app.
code:
#implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationPortrait;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
}
ios6 uses mainly the plist options for autorotation. Try to setup the supported orientations fields.
Have you tried to modify the plist?
Please check your project targets summery is like below

App displaying in landscape when on iPhone mode on iPad

When my app launches on the iPhone, it displays in portrait, as it should. When it launches on the iPad in iPhone mode, it displays in landscape, if rotated that way. This is the code I have:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate {
return NO;
}
For iOS 6, have you try :
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
In your project info.plist file, remove the landscape flags from supported orientations list.
In Xcode select one of your target then under summary tab make sure you have selected only UIpotrait orientation and rest all of them are unslected, then choose info tab and see that under "supported interface orientations" row it should have only UIpotrait orientation .Now open your delegate file and for ios5 this method works
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
but this method is deprecated for iOS6 to override it for iOS6
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}

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.

i want to know device orientation when the view will appear IOS 6

I want to know device orientation when the view will appear.
Previously it was possible using the shouldAutorotateToInterfaceOrientation method but in IOS 6 it is deprecated.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation==UIInterfaceOrientationPortrait ||interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown ) {
DashBtn.hidden=NO;
}
else
{
DashBtn.hidden=YES;
if (self.popoverControllerMain) {
[self.popoverControllerMain dismissPopoverAnimated:YES];
}
}
return YES;
}
I checked all the post
i.e making rootviewcontroller and
- (BOOL)shouldAutorotate {
return NO;
}// this method is not called
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Create category of UINavigationController and add below code #implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
Try using
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
instead of
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
refer http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html for details.
The best option to know the device orientation is
[[UIApplication sharedApplication] statusBarOrientation]

Interface orientation in iOS 6.0

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