Force landscape for one view controller ios
Asked Answered
M

3

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.

Mccaslin answered 23/8, 2013 at 18:16 Comment(13)
shouldAutoRotate, [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];, nadaMccaslin
What version of iOS are you targeting?Choric
Sorry iOS 6... I tried this as well: http://stackoverflow.com/questions/14633213/force-portrait-orientation-while-pushing-from-landscape-view-controllerMccaslin
Also, I've read your book. It's an honor to have you help me out!Mccaslin
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?Imbibition
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!Mccaslin
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.Mccaslin
@Imbibition 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.Mccaslin
Are you using view containment at all? I couldn't confidently state that UINavigationController, etc, would handle changes in supported orientation gracefully.Imbibition
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 helpsMccaslin
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:.Choric
Also, thanks! That comment made my night.Choric
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!Mccaslin
A
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.

Airspace answered 11/3, 2014 at 0:19 Comment(0)
P
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.

Pryce answered 26/8, 2013 at 6:25 Comment(1)
The "-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window" makes little sense. Instead, implement it as "- (NSUInteger)supportedInterfaceOrientations" in your ViewController.Airspace
S
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);
Saidel answered 21/11, 2014 at 11:14 Comment(1)
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.Nupercaine

© 2022 - 2024 — McMap. All rights reserved.