10

I've tried to following methods to force landscape on one my views:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotate{
    return YES;
}

Neither did work. Note that I'm testing on the simulator and on an iPad.

Thanks

diogo.appDev
  • 1,595
  • 5
  • 16
  • 30

3 Answers3

22

Here is how I forced one of my views to be Landscape using NavigationViewController:

  1. Implemented this answer: https://stackoverflow.com/a/12662433/2394787

  2. Imported message in the View controller: objc/message.h

  3. Added this line of code in the viewDidLoad method:

objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);

Hope it helps someone.

Community
  • 1
  • 1
diogo.appDev
  • 1,595
  • 5
  • 16
  • 30
  • Will this logic be approved for Apple? – Mc- Mar 21 '14 at 16:46
  • It looks like all I needed was steps 2&3 (1 seems to be optional) – Jeef Apr 17 '14 at 12:13
  • with step 1 you prevent the user from going portrait again – diogo.appDev Apr 17 '14 at 14:52
  • I have it running in two apps – diogo.appDev Apr 20 '14 at 21:46
  • this seems to work in the iOS simulator but not on the actual device? – just.jimmy Aug 01 '14 at 05:23
  • This is definitely wrong. If you're trying to force a particular interface orientation you want to call `[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;` Apple has provided two different enums that are similar, but very different: UIDeviceOrientation and UIInterfaceOrientation. The device property is read-only, because you can't force the physical device to change orientation! The statusBarOrientation *is* writable, and will do what you were trying to do with setValue:forKey: – Dan Jackson Jun 11 '15 at 19:12
7

The accepted answer doesn't seem to work on iOS 7.1.2. (It works on the simulator but does not work while running on a device.)

This seems to work though:

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"];
just.jimmy
  • 954
  • 1
  • 8
  • 15
1
int shouldShowInLandscape = 0;

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(forceToLandscape:)
                                                 name:@"yourNameNotification"
                                               object:nil];

}

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
              return UIInterfaceOrientationMaskLandscapeLeft;
    } else {
            if(shouldShowInLandscape)
            {
                return UIInterfaceOrientationMaskLandscape;
            }
            return UIInterfaceOrientationMaskPortrait;
    }
}

-(void) forceToLandscape:(NSNotification*) theNot
{
    shouldShowInLandscape = [[theNot object] intValue];

}

// from the UIViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"1"]];
}

-(void) viewDidDisappear:(BOOL)animated
{

        [[NSNotificationCenter defaultCenter]
     postNotificationName:@"yourNameNotification"
     object:[NSString stringWithFormat:@"0"]]; 

}

// remove you notificationCenter

devnull69
  • 16,402
  • 8
  • 50
  • 61
Carlos Avalos
  • 247
  • 4
  • 8