10

I've looked at several answers to questions similar but none of the answer worked. I have an app where I need everything portait except for one photo viewer I have. In the supported orientations section of the targets menu I only have portrait. How do I force my one view to be landscape. It is being pushed onto the stack from a nav controller but I'm using storyboards to control all that.

Kowser
  • 8,123
  • 7
  • 40
  • 63
Evan Stoddard
  • 718
  • 1
  • 7
  • 21
  • `shouldAutoRotate`, `[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];`, nada – Evan Stoddard Aug 23 '13 at 18:22
  • What version of iOS are you targeting? – Jeff Kelley Aug 23 '13 at 18:23
  • Sorry iOS 6... I tried this as well: `http://stackoverflow.com/questions/14633213/force-portrait-orientation-while-pushing-from-landscape-view-controller` – Evan Stoddard Aug 23 '13 at 18:25
  • 3
    Also, I've read your book. It's an honor to have you help me out! – Evan Stoddard Aug 23 '13 at 18:26
  • Have you tried declaring that the app supports both landscape and portrait but having all the other view controllers return `supportedInterfaceOrientations` of just `UIInterfaceOrientationMaskPortrait` and the photo viewer return `UIInterfaceOrientationMaskLandscape`? – Tommy Aug 23 '13 at 20:07
  • No I haven't. I'm not at work right now so I can't work on it until monday but I will definitely try that. Thank you! – Evan Stoddard Aug 23 '13 at 22:32
  • Now do I have to make a nav controller class and put this line in as well or can I just put it in the visible classes. – Evan Stoddard Aug 23 '13 at 22:33
  • @Tommy I tried that with a simple app but it still had no effect. The portrait didn't remain in that orientation when I rotated the device. – Evan Stoddard Aug 23 '13 at 22:42
  • Are you using view containment at all? I couldn't confidently state that `UINavigationController`, etc, would handle changes in supported orientation gracefully. – Tommy Aug 23 '13 at 23:41
  • Honestly I have no idea. First off I'm using storyboards and secondly I'm I have a nav controller for mot of the tabs. I don't know if that helps – Evan Stoddard Aug 23 '13 at 23:46
  • Are you using a `UITabBarController`? It does some weird things with rotation. If you need to present something in landscape with a tab bar controller, can you display it modally? That should get you out of having to work with the tab bar controller. Then you just need to implement `-supportedInterfaceOrientations` and `-shouldAutorotateToInterfaceOrientation:`. – Jeff Kelley Aug 24 '13 at 01:51
  • 1
    Also, thanks! That comment made my night. – Jeff Kelley Aug 24 '13 at 01:51
  • Yeah I can do it modally. That's no big deal. I'll check on that monday. Just to reiterate though, I do have portrait set as the only acceptable orientation for the project. And its my pleasure. Your book really got me started and got me a great job! – Evan Stoddard Aug 24 '13 at 02:06

3 Answers3

14

Since the answer seems to be hidden in the comments of the question and since ArunMak's answer is quite confusing, I'll just offer what I found out:

All I had to do was to add this function to my custom UIViewController subclass for the view:

- (NSUInteger)supportedInterfaceOrientations {
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        // iPad: Allow all orientations
        return UIInterfaceOrientationMaskAll;
    } else {
        // iPhone: Allow only landscape
        return UIInterfaceOrientationMaskLandscape;
    }
}

Note that the project needs to allow all orientations (that is: Portrait, Landscape Left, Landscape Right - but NEVER Upside Down on an iPhone!).

If you want to limit some or most views to Portrait, you need to implement the above method in every of those view controllers (or use a common super class for it and subclass all others from it) — if you limit the Device Orientation in the Info.plist to just Portrait, the app will never even think of going into landscape.

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
2

Yes this is possible, you can use this code:

    -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        return UIInterfaceOrientationMaskLandscape;
    }

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
        return orientation==UIInterfaceOrientationMaskLandscape;
    }

OR

Try this method in your app delegate

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (sglobalorientation isEqualToString:@"AllOrientation"]) {
        return UIInterfaceOrientationMaskLandscape;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

you need to change the variable value sglobalorientation to that string value AllOrientation before you move to that Landscape view controller

and in your Landscape view controller, use this code in your view will appear

 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
    DigitalSignatureViewController *digisign = [[DigitalSignatureViewController alloc]init];
    [self presentModalViewController:digisign animated:NO];
    [self dismissModalViewControllerAnimated:NO];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

and again when you move to next view controller change the sglobalorientation string value and follow the same step in your next view controller.

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
ArunMak
  • 398
  • 2
  • 12
  • The "-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window" makes little sense. Instead, implement it as "- (NSUInteger)supportedInterfaceOrientations" in your ViewController. – Thomas Tempelmann Mar 11 '14 at 00:07
0

Lets try this code:

    [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
self.navigationController.view.center = CGPointMake(([UIScreen mainScreen].bounds.size.width/2), [UIScreen mainScreen].bounds.size.height/2);
CGFloat angle = 90 * M_PI / 180;
self.navigationController.view.transform = CGAffineTransformMakeRotation(angle);
self.navigationController.view.bounds = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.height , [UIScreen mainScreen].bounds.size.width);
msmq
  • 1,298
  • 16
  • 28
  • This solution is wrong. Keyboards will be not rotated when you try to edit a field. The same will happen with system alerts for sure. Also status bar does not work. – Oleh Kudinov Mar 01 '16 at 10:36