Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only ONE VIEW landscape mode

I finished my iOS app but I need to set only ONE view to landscape mode, the rest of the views can only be seen in portrait mode.

I'm using Xcode 5.1 and I created all of my Views by dropping in my storyboard View Controllers from the right panel, so if you are going to tell me to write some code somewhere, please tell me exactly where I need to write it.

I read one solution here UINavigationController Force Rotate but I don't know where to write that code. Do I need to create one UIViewController manually?

like image 731
maeq Avatar asked Jul 24 '14 07:07

maeq


People also ask

How do you fix landscape mode on Iphone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.


2 Answers

Swift

AppDelegate.swift

internal var shouldRotate = false func application(_ application: UIApplication,                  supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {     return shouldRotate ? .allButUpsideDown : .portrait } 

Your landscape view controller

let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.shouldRotate = true // or false to disable rotation 

Objective-C

AppDelegate.h

@property (assign, nonatomic) BOOL shouldRotate;

AppDelegate.m

- (UIInterfaceOrientationMask)application:(UIApplication *)application  supportedInterfaceOrientationsForWindow:(UIWindow *)window {     return self.shouldRotate ? UIInterfaceOrientationMaskAllButUpsideDown                              : UIInterfaceOrientationMaskPortrait; } 

Your landscape view controller

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; [appDelegate setShouldRotate:YES]; // or NO to disable rotation 
like image 161
Yaroslav Avatar answered Sep 19 '22 00:09

Yaroslav


I am gonna suppose you are targeting iOS 7 here (using XCode 5.1, I think I am right).

First, you have to understand that in order to open even just one view out of over 40 in landscape, your app should allow both landscape and portrait interface orientations. It is the case by default, but you can check it in your target's settings, General tab, Deployment Info section (see screenshot below).

enter image description here

Then, because you allowed both landscape and portrait for the entire app, you will have to tell every portrait-only UIViewController that it should not autorotate, adding this method's implementation:

- (BOOL)shouldAutorotate {   return NO; } 

Finally, for your specific landscape-only controller, and because you said you are presenting it modally, you can just implement these methods:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {   return UIInterfaceOrientationLandscapeLeft; // or Right of course }  - (UIInterfaceOrientationMask)supportedInterfaceOrientations {   return UIInterfaceOrientationMaskLandscape; } 

Hope this will help,

like image 43
Zedenem Avatar answered Sep 18 '22 00:09

Zedenem