ios default orientation with autorotation - ios

I'm developing an app which has 100 UIViewController almost. The app always run in portrait. But now, I want to allow rotate the app, buy only two screen (2 UIViewController).
So, in General settings, then in Deployment info, I mark:
Device Orientation -> Portrait
Device Orientation -> LandscapeLeft
Device Orientation -> LandscapeRight
And now, for example
(BOOL)shouldAutorotate return YES by default.
How avoid to override this method in every UIViewController? Which is the best solution? For me, its very difficult create a UIViewController superclass for all my UIViewController because some extends from TableViewController, another from UIViewController, another from...
Is there anyway to mark shouldautorate NO by default, supportedInterfaceOrientations only Portrait by default? And then, in a UIViewController that I want to rotate, override shouldautorotate, supported... I desire this option for maintenance of the project. No worry about a new UIViewController overrides this methods for no rotate or a new UIVC extends for a UIVC superclass that overrides this methods etc. Just, UIVC that I want rotate override the methods, otherwise you don't do anything.
Thanks, I hope you understand my question.

Related

How to force set Landscape Orientation with UINavigationController (Swift 2.0 iOS 9)

I tried to find a solution but so much information which doesn't work. My last try was using the following:
UIApplication.sharedApplication().setStatusBarOrientation(UIInterfaceOrientation.LandscapeRight, animated: false)
This however, was deprecated from iOS 9 and couldn't find any way to force rotate with UINavigationController. My app mainly uses Portrait Orientation and only one view needs to be Landscape. I need to force Landscape on one View and rest to keep as Portrait. Any help would be highly appreciated!
Some of the questions I checked are:
Setting device orientation in Swift iOS
How do I programmatically set device orientation in iOS7?
Why can't I force landscape orientation when use UINavigationController?
If this is something you really want to do, subclass UINavigationController then add this code:
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .Landscape
}
Trying to force an orientation imperatively is unwise; it's better to tell iOS what you want (as above) then let it calculate the orientation as best it can.
We had to do this same thing in our app as well. Initially we worked with a hack. But eventually we switched the "Landscape" VC to a modal rather than part of navigation view controller stack. I would suggest you do that. But if you really want to, here is how you do it.
Subclass Navigation VC.
in supportedInterfaceOrientaions check for VC type & return appropriate orientation (landscape for one you want, portrait for rest)
This itself wont autorotate that VC to landscape, so here is the hack.
In viewDidLoad/viewDidAppear of "landscape" VC, push another generic VC object & pop it subsequently
UIViewController *c = [[UIViewController alloc]init];
[self presentViewController:c animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
This used to work in iOS7 & we switched to modal after that. so this might now work in later versions.

Allow multiple orientations for only one UIViewController

I want the general orientation of my app to be portrait, but for one specific UIViewController (VC2B) I want the user to be able to tilt the device for landscape orientation. (VC2B has a navigation bar)
I have tried different types of code but without luck. Ideally I would like to to enable rotation in only UIViewController (VC2B). But this seems impossible, instead it seems you have to enable all the orientations in the General section and then turn them off programatically - is this really true?
Anyway I have tried various things
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientation.LandscapeLeft.rawValue) | Int(UIInterfaceOrientation.LandscapeRight.rawValue) | Int(UIInterfaceOrientation.Portrait.rawValue)
}
The below answer suggest overriding shouldAutorotate to false, but that does not work if the page is loaded in landscape mode, then it is just stuck on landscape.
Allow all orientations for only one view controller
Question: How can I in a tidy way, without adding code to all my UIViewControllers, allow multiple orientations for only one UIViewController?
Do the following as I did this when I faced same situation:
enable the desired orientations in the General Info Screen
in each ViewController.swift, override the autorotation function with either true or false (true for the one that should rotate, false for the others)

Can't restrict UIViewController to Portrait only

I'm embedding my app in a UINavigationController, I want most of myViewControllers except one to be Portrait, I've read a lot of questions but could not find a correct answer that works for me.
In my target I'm selecting Device Orientation : Portrait, Landscape Right
I'm adding this to my first ViewController:
-(BOOL)shouldAutorotate{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations{
return (UIInterfaceOrientationPortrait);
}
But when I rotate the device left the ViewController rotates as well.
Why is it rotating?
You can't easily do in iOS 7 what you're describing. A UINavigationController does not consult its children as to what rotations they like; whatever the permitted rotations of the UINavigationController, those are the permitted rotations of the app, regardless of which child happens to be showing at that moment.
The only really legal and built-in way to force rotation is to use a presented ("modal") view controller that takes over the screen. Its rotation settings are consulted because it is now in charge of the screen.

UIImagePickerViewController in landscape mode

I am working on app which is totally in Landscape mode. On one of the view, I need to open the camera(UIImagePickerController) with overlay view. But When I click the button to open camera it get crashed with this issue i.e. reason:
'Supported orientations has no common orientation with the
application, and shouldAutorotate is returning YES'
I also put this line in viewcontroller but no effect.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Please give me some idea to make it working.
If you are using a UINavigationController as base view controller , that code snippet you provided simply doesn't work. Because you are adding that code to a UIViewController but it's already embeded in a UINavigationController.
To overcome this issue, you must create a subclass of UINavigationController and add that landscape related code to that subclass. Then assign this subclass to your Storyboard's base Navigation Controller. Then this issue will be solved.
This is your navigation controller sub class' .h file
#interface YourCustomNavigationController : UINavigationController
#end
And add this to your navigation controller subclass' .m file
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return YES ;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
And make sure to assign this class to your base Navigation controller using interface builder's identity inspector.
Project settings,
EDIT:
You can present a UIImagePickerController in landscape mode. Regardless of what documentation says, you can actually subclass it and can override it's functionality to work on landscape mode. (atleast its working on iOS 7)
To do this, you must create a subclass of UIImagePickerController and add above lines to that subclass. Project settings must be same as screenshot (atleast one landscape mode and one portrait mode must be ticked).
Then use that subclassed ImagePicker controller when you presenting the camera. It would successfully load camera in landscape mode. But standard camera controls will be misaligned and messed up. So better to hide defaultControlls (picker.showsCameraControls = NO;) just like you've done and use an overlay view instead.

iOS >> Device Orientation >> Screen is Not Supporting Upside Down

I have a screen that supports Device Orientation.
Everything is working fine except for the fact that when I rotate the device upside down (home button at top), the rotation doesn't work (it's stuck on the last landscape settings).
I know of several places needed be updated to support this:
In the VC itself, I added the methods:
In the Project Target, I updated as follow:
In the Storyboard VC Scene, I updated as follow:
What am I missing here?
You also have to allow rotating to all orientations in every parent view controller of the current main view controller. For example, if your view controller is in navigation controller, try subclassing it and override the same methods as in your example.
Edit: As #JordanC mentioned, since iOS 7 you can implement UINavigationControllerDelegate method to return custom supported orientations:
- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
As #eGanges mentioned the key point could be to subclass your UITabBarController (and override supportedInterfaceOrientations) if that is your initial view controller, in that case this is the only controller you should subclass (and of course you should add all the supported interface orientations to your app Info.plist file UISupportedInterfaceOrientations key)
Have you tested on real device?
anyway try this:
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}

Resources