Only ONE VIEW landscape mode

maeq picture maeq · Jul 24, 2014 · Viewed 44.4k times · Source

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?

Answer

Yaroslav picture Yaroslav · Jul 24, 2014

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