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
}
force landscape ios 7
Here is how I forced one of my views to be Landscape using NavigationViewController:
Implemented this answer: https://stackoverflow.com/a/12662433/2394787
Imported message in the View controller: objc/message.h
Added this line of code in the viewDidLoad method:
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
Hope it helps someone.
How to force view controller orientation in iOS 8?
For iOS 7 - 10:
Objective-C:
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
[UINavigationController attemptRotationToDeviceOrientation];
Swift 3:
let value = UIInterfaceOrientation.landscapeLeft.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
Just call it in - viewDidAppear:
of the presented view controller.
Related Topics
How to Center Align the Cells of a Uicollectionview
Automaticallyadjustsscrollviewinsets Not Working
Current Week Start and End Date
Uialertview/Uialertcontroller iOS 7 and iOS 8 Compatibility
How to Store an Image in Core Data
Xcode 8 Build Crash on iOS 9.2 and Below
How to Reload Tableview from Another View Controller in Swift
"Untrusted App Developer" Message When Installing Enterprise iOS Application
Error: Initializer for Conditional Binding Must Have Optional Type, Not 'String'
iOS UI Testing on an Isolated View
"Reading from Public Effective User Settings" in iOS 10
Autolayout + Rtl + Uilabel Text Alignment
iOS How to Detect When App Was Removed from Process
How to Determine Height of Uicollectionview with Flowlayout
Tutorial on How to Drag and Drop Item from Uitableview to Uitableview