2

I have menu that is portrait only and detail view (with UIWebView) that can be portrait or landscape.

When I enter detail view and rotate device landscape and go back from that screen in that way to menu which should only be portrait then menu is in landscape orientation (along with status bar and navigation bar).

Is there a way to avoid this and force screen to be rotated to desired (supported) orientation?

Binarian
  • 12,296
  • 8
  • 53
  • 84
Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79

2 Answers2

2

Proposed solution by @kkumpavat didn't worked for me or it wasn't clear enough for me to understand it. I did more searching and found solution which uses:

    // http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
    // http://openradar.appspot.com/radar?id=697
    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:")
                                   withObject:UIInterfaceOrientationPortrait];

but this was generating annoying warnings and I've swapped it with:

//force update of screen orientation
if (self.interfaceOrientation != [self preferredInterfaceOrientationForPresentation]) {
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: [self preferredInterfaceOrientationForPresentation]]
                                forKey:@"orientation"];
}

called from NavigationController's viewDidAppear.

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
  • the setValue:forKey: approach is beautiful. But it also is a private API. Will this cause my application to get rejected? (Using it for iOS 8) – Cutetare Jul 21 '14 at 14:21
  • @Cutetare I'm also curious about that. Did anyone try this? – alper_k Sep 08 '14 at 12:58
  • I ended up using another approach for iOS8, see http://stackoverflow.com/questions/24907239/iphone-interface-orientation-on-ios8 – Cutetare Sep 08 '14 at 20:11
  • 1
    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:09
-1

Change viewWillAppear of menu viewController as below

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIViewController *controller = [[UIViewController alloc] init];
    [self presentViewController:controller animated:NO completion:nil];
    [self dismissViewControllerAnimated:NO completion:nil];
}
kkumpavat
  • 452
  • 2
  • 10