shouldAutorotate -> false, still simulator rotates - ios

I'm making an app which supports landscape orientations only when playing a video. Otherwise, all the scenes support only portrait orientation. I've checked portrait, landscape left and right in project settings. I've written the following code in the ViewControllers where I want to restrict to only portrait.
override func viewWillAppear(animated: Bool) {
let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
return UIInterfaceOrientation.Portrait.rawValue
}
Still, when I press (Command + right or left arrow), the simulator rotates. I don't have a device, so I have to test it in simulator.
Please help! Thank you!

It's the parent navigation controller that decide if its content should rotate or note.
You will need to override UINavigationController and had something like this
override func shouldAutorotate() -> Bool {
return self.topViewController.shouldAutorotae()
}
override func supportedInterfaceOrientations() -> Int {
return self.topViewController.shouldAutorotae()
}

Two things to check. First, check your info.plist file and make sure that you have portrait deleted for both iPhone and iPad. I was having the same issue and it was because I hadn't deleted the iPad "Portrait" options. See here:
Second, the below code can help. However, it may not work if you have a navigation controller.
override var shouldAutorotate: Bool {
return true
}

Related

How to stop the device Orientation changing

I'm working on an app that is to be in portrait mode when it is launched. After that, I add an SDK for some purpose to my project. In the process of the integrating the SDK needs both Portrait and Landscape Right checked in the General pane. So I set it as given in the SDK integration manual.
and after that when I run my project in a device and rotate it to right the app device orientation is changing. How can I set the orientation is fixed without disturbing the SDK.
if you're having trouble to set orientation right after the app launches then you might need to stop orientation for each view controller or one of it's base navigation controller
Swift 3:
class YourBaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
}
override var shouldAutorotate: Bool {
return false
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return .portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return .portrait
}}
Use YourBaseNavigationController as your navigation controller.Basically all view controller within this navigation controller will be force to support portrait mode only.
unmark the checks Landscape left and landscape right
You need to disable orientation through code.
Add this code in your RootViewController:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);//set 'return NO'; for stop orientation
}
Swift :
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}

Manage Device Orientation - IOS

I'm really new to IOS develop and before ask this question I already tried literally all the answer on stackoverflow but couldn't find anything that work (probably I fail somewhere).
I want to Disable the possibility of rotate the device view in all my App and I want to decide in every ControllerView if use portrait or landscape.
How can I do that with Swift?
you should try this on every ViewController an set it with the possibility to rotate, and what orientation should support.
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
I got this error:

I want only one screen in portrait mode while all other in both landscape and portrait mode ios 9 application

I want to Restrict one page in portrait orientation while whole application in both landscape and portrait orientation how to achieve this functionality. I have tried old code from stack over flow but it's not working.
If any one solved such problem then please help me thanks in advance.
Add this snippet to the ViewController which should not rotate:
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}

Force orientation to be Portrait for one view and both for another

My design is as follows: Menu -> App -> Back to Menu
I want my Menu to be 'Portrait ONLY'. My AppView can be Portrait & Landscape.
Currently in my MenuController, I have:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
and in my AppView Controller I have:
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
However, my Main menu is rotating in all directions. Anything I am missing here? Thanks!
Yes you need to also change it in plist. You change the property in target of the project. Go to Target, in general tab go to deployment info -> device orientation......
Further you need to subclass UINavigationController and override it:
-(NSUInteger)suppoertedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
This will make sure that the viewController being displayed by navigation is in syn

iOS lock specific ViewController to specific orientation

I want to lock the device's orientation in a specific ViewController to only portrait but none of the available solutions are working. It is embedded in a UINavigationController.
I deployed it to iOS7 and in the general settings, both landscape and portrait are checked because I want it to rotate for other VCs other than the specific one.
extension UINavigationController {
public override func shouldAutorotate() -> Bool {
return visibleViewController.shouldAutorotate()
}
public override func supportedInterfaceOrientations() -> Int {
return visibleViewController.supportedInterfaceOrientations()
}
}
this portion of the code is in my ViewController class
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientation.Portrait.rawValue)
}
"In iOS 8, all rotation-related methods are deprecated" (See documentation on UIViewController). This could be why it isn't working for you.
I am thinking that Apple has done this to keep the users' experience more consistent throughout the app.
I figured out the problem which is since I am using SWRevealViewController, the parent VC overrides the subview's rotate settings.
Thus I just subclass it and adjust the autorotate and orientation settings as intended.

Resources