4

How to rotate screen to landscape ?

Can you suggest simple code ?

hello_there_andy
  • 2,039
  • 2
  • 21
  • 51
Ferdinand
  • 1,193
  • 4
  • 23
  • 43
  • This is a duplicate of http://stackoverflow.com/questions/402/iphone-app-in-landscape-mode or http://stackoverflow.com/questions/1437636/how-to-autorotate-from-portrait-to-landscape-mode – Brad Larson Feb 17 '10 at 13:26

5 Answers5

2

It's trickier than you first think! After much discussion this blog post (with a link to further discussion afterwards) contains the cleanest answer:

How to Switch to Landscape Mode at will

rougeExciter
  • 7,435
  • 2
  • 21
  • 17
2

in the uiViewController you should have the method

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
 return YES;
}

in order to rotate automatically.

If your app has an uiTabBarController then you have to subclass the UITabBarController and add the method to it also.Something like this:

@interface MyTabBarController : UITabBarController {

}

@end

#import "MyTabBarController.h"

@implementation MyTabBarController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

@end
Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71
  • – shouldAutorotateToInterfaceOrientation: Deprecated in iOS 6.0. See [this](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:). – JohnK Jun 20 '13 at 23:04
0

This howto describes how you can enable auto rotation in your application: http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-HANDLE_AUTOROTATION_OF_MY_USER_INTERFACE

This howto describes how you can start your application in landscape mode: http://developer.apple.com/iphone/library/codinghowtos/UserExperience/index.html#GENERAL-START_MY_APPLICATION_IN_LANDSCAPE_MODE

Lauri
  • 4,670
  • 2
  • 24
  • 17
0

Thanks SorinA. Needing UITabBarController shouldAutorotateToInterfaceOrientation: gave me all sorts of frustrations since it is not a class that I was subclassing for any reason (i.e., the default functionality was fine for my app).

As a lighter-weight solution than subclassing I used a category (maybe its all the same, but it seems lighter weight).

@interface UITabBarController (WithRotation)
@end

@implementation UITabBarController (WithRotation)
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation {
    return YES;
}
@end

p.s. If you only want to support some orientations, test the interfaceOrientation and only return YES in the appropriate orientations.

p.p.s. What is the info.plist Supported interface orientations for? The only thing that seems to matter is what is returned from shouldAutorotateToInterfaceOrientation:

LavaSlider
  • 2,494
  • 18
  • 29
  • – shouldAutorotateToInterfaceOrientation: Deprecated in iOS 6.0. See [here](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:). – JohnK Jun 20 '13 at 23:05
0

Just implement this in your view controller file, and you're done.

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
     return YES;
}
NikiC
  • 100,734
  • 37
  • 191
  • 225
shy
  • 107
  • 1
  • 5
  • 14
  • – shouldAutorotateToInterfaceOrientation: Deprecated in iOS 6.0. See [this](http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:). – JohnK Jun 20 '13 at 23:04