Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Orientation to Portrait with Swift

Tags:

ios

swift

I have an app where some of the screens should be in portrait mode and some of them in landscape. For example, main menu is portrait and there is a camera button. The camera should be available only in landscape. I am using Storyboard and NavigationController. Where and what should I do to achieve this?

I already tried to include

shouldAutorotate() -> Bool {return false or return true} 

neither worked. I have set the

supportedInterfaceOrientation 

for every viewcontroller I have. Nothing helped. I need a function what I can call to force the screen into landscape/portrait mode.

like image 456
MegaX Avatar asked Dec 06 '22 22:12

MegaX


1 Answers

Use this line of code in your ViewDidLoad (or wherever you want).

Swift 1 UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")

Tested 3 minutes ago in Swift 1.2 Xcode 6.3

Swift 3 (Thanks to Fa.Shapouri)

UIDevice.current.setValue(UIInterfaceOrientation.landscapeLe‌​ft.rawValue, forKey: "orientation")
like image 153
gutenmorgenuhu Avatar answered Dec 26 '22 05:12

gutenmorgenuhu