Force landscape mode in one ViewController using Swift
It may be useful for others, I found a way to force the view to launch in landscape mode:
Put this in the viewDidLoad():
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
and,
override var shouldAutorotate: Bool {
return true
}
Rotation only in one ViewController
This is for Swift 4 and Swift 5. You can use the follow code in your AppDelegate.swift :
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
guard let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController),
(rootViewController.responds(to: Selector(("canRotate")))) else {
// Only allow portrait (standard behaviour)
return .portrait;
}
// Unlock landscape view orientations for this view controller
return .allButUpsideDown;
}
private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
guard rootViewController != nil else { return nil }
guard !(rootViewController.isKind(of: (UITabBarController).self)) else{
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
}
guard !(rootViewController.isKind(of:(UINavigationController).self)) else{
return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
}
guard !(rootViewController.presentedViewController != nil) else {
return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
}
return rootViewController
}
You can then make a custom UIViewController rotate by overriding shouldAutorotate
Force landscape orientation in one View
I solved this by orientating on bmjohns answer on a similar question:
- Add some code in AppDelegate (this part i had already)
- Make a struct and func which does the job (small adjustments for XCode 11 and SwiftUI were needed)
- Execute the func onAppear of your View
Only ONE VIEW landscape mode
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).
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,
Related Topics
Why Maskstobounds = Yes Prevents Calayer Shadow
How to Manually Deprecate Members
What Are the Device-Width CSS Viewport Sizes of the Iphone6 and iPhone 6 Plus
Using Uiimagepickercontroller in Landscape Orientation
How to Save a Uicolor with Userdefaults
Animate Uilabel Text Between Two Numbers
Avcapturesession with Multiple Previews
Gradient Polyline with Mapkit iOS
How to Fetch All Contacts Record in iOS 9 Using Contacts Framework
Add Entry to iOS .Plist File via Cordova Config.Xml
How to Center a Popoverview in Swift
Fix CSS Hover on Iphone/Ipad/Ipod
How to Use Nsurlsessiondatatask in Swift
Optional Chaining in Swift Closure Where Return Type Has to Be Void