Autorotation in iOS6 - iphone

I am updating my app for iOS 6 and having issues with the changes to autorotation. My app has a bunch of view controllers and all of them should only support the portrait layout except 1 which should support all 3 orientations except upside down.
If I add the application:supportedInterfaceOrientationsForWindow: method to the app delegate do I have to add conditions there to check if im displaying the one VC I want to be able to rotate?
The documentation states that if I implement supportedInterfaceOrientations on a VC it should override the app delegate method but this doesn't appear to be the case. I have an log statement in the method on the child VC and it is called once when the VC loads but its not called when I rotate the device, but the method in the app delegate is.
If I completely remove the method from the app delegate the orientation of my VC's seems to be completely dependent on my apps supported interface orientation settings. This of course seems to be due to the method supportedInterfaceOrientations being called once on creation of the VC but never when the device is rotated.
Does anyone have any ideas or suggestions? It would be much appreciated.

Replace
[window addSubview:viewController.view];
with
window.rootViewController = viewController;

You also need to override - (BOOL) shouldAutorotate and return "YES". This makes it so you declare what orientations your VC supports with "supportedInterfaceOrientations" and then on rotation it should call "shouldAutorotate". If you have any navigation controller or tabbar you may need to subclass those to do the same thing within them. I had this issue recently myself.

try this...
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
// here to implement landscope code
}
else
{
// here to implement setframePortrait
}
}

Related

Handling autorotation for one view controller in iOS7

I've read many answers on SO but I can't seem to get autorotation working on iOS7.
I only need one view controller to rotate, so I don't want to set rotation settings in my Info.plist.
As I understand Apple's documentation, a single view controller can override global rotations settings (from Info.plist) by simply overriding two methods. Info.plist is set to only allow Portrait, and my view controller implements the following methods:
- (NSUInteger)supportedInterfaceOrientations
{
NSLog(#"%s", __PRETTY_FUNCTION__);
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate
{
NSLog(#"%s", __PRETTY_FUNCTION__);
return true;
}
I'm seeing those NSLog statements upon rotation but nothing rotates.
If I do configure Info.plist with the proper rotation settings, my view will rotate, but not if I try and rely on my view controller.
Not sure if it matters, but the view I'm trying to rotate is from a .xib using auto layout.
Also, my ViewController is being presented modally and is contained in a navigation controller. I've tried just presenting the view controller by itself and that doesn't work. I've also tried adding a category to UINavigationController to get it's autorotation directions from it's topViewController.
In my case, I had a new iOS7 app with about 30 view controllers created already. I needed auto rotation on just a single modal view controller. I didn't want to have to update the preexisting view controllers.
I selected the orientations I wanted in the plist:
Then I added a category to my app delegate on UIViewController:
#implementation UIViewController (rotate)
-(BOOL)shouldAutorotate {
return NO;
}
#end
Then in the single modal view controller I WANTED to rotate I added this method:
-(BOOL)shouldAutorotate {
return YES;
}
I also discovered, that if my view controller wasn't a modal VC I would need to add category methods on UINavigationController instead, for all VCs that were subsequent to the root view controller, as part of the navigation stack of view controllers - similar to this: https://stackoverflow.com/a/20283331/396429
Simple but it work very fine. IOS 7.1 and 8
AppDelegate.h
#property () BOOL restrictRotation;
AppDelegate.m
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
viewDidLoad
[self restrictRotation:YES]; or NO
You need to set the plist value to all possible values, then limit them as you see fit (in the Navigation Controllers and TabBar Controllers. From the UIViewController class description:
In iOS 6 and later, your app supports the interface orientations
defined in your app’s Info.plist file. A view controller can override
the supportedInterfaceOrientations method to limit the list of
supported orientations. Typically, the system calls this method only
on the root view controller of the window or a view controller
presented to fill the entire screen; child view controllers use the
portion of the window provided for them by their parent view
controller and no longer participate directly in decisions about what
rotations are supported. The intersection of the app’s orientation
mask and the view controller’s orientation mask is used to determine
which orientations a view controller can be rotated into.
I've faced such problem - had only one landscape view in my app.
I've used below code to to handle that.
#import "objc/message.h"
-(void)viewWillAppear:(BOOL)animated{
objc_msgSend([UIDevice currentDevice], #selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
}
I know this is old but I ended up in a more unique situation where we have 50+ ViewController all over the app that I refused to go through and modify and support the same orientation in all of them but one or 2. Which brings me to my answer. I created a UIViewController category that overrides - (BOOL)shouldAutorotate to always return NO or YES depending on device type etc. (this can be done with supported interface orientations too). Then on the ViewControllers I wanted to support more then just portrait, I swizzled shouldAutorotate to return YES. Then forced the orientation change when the view is dismissed on the parent ViewControllers viewWillAppear method using:
[[UIDevice currentDevice] setValue:#(UIInterfaceOrientationPortrait) forKey:#"orientation"].
When all was said and done, I accomplished everything I wanted on a few ViewControllers with < 30 lines of code using a macro for swizzling. Had I done it by replacing shouldAutorotate and supportedInterfaceOrientations on all of the VC's in the application I would have ~250 extra lines of code. and a lot of grunt work adding it in the first place.

How to allow a particular view controller to support for both landscape and portrait orientation

My application is navigation based application which is supporting iOS 6. Except a view controller all others will support only portrait mode. Particular view controller alone has to support both landscape and portrait orientations.
I searched lot there are tons of questions for this but none of the answer is suitable for me. If some one knows, kindly guide me
I set the orientation as Portait in Project -> Targets -> Summary -> Supported orientation
First you should use methods for the iOS6 presented in UIViewController documentation if you are making your app for iOS6. Orientation method like shouldAutorotateToInterfaceOrientation is deprecated in iOS6, alternate method for iOS6 is shouldAutoRotate. You should only use the old method if your app is supporting also iOS5.
Second If you are using UINavigationcontroller in your application and you need to have different interface orientations then navigationController could mess up the interface orientation in the application. Possible solution (worked for me) is to implement a custom UINavigationController and override the interface orientation methods within that custom UINavigationController class, this will make your viewControllers rotate according to the orientation you set because your controllers are pushed from the UINavigationController. Don't forget to add those methods in your particular viewController also.
CustomNavigationController.h
#interface CustomNavigationController : UINavigationController
#end
CustomNavigationController.m
#implementation CustomNavigationController
//overriding shouldRotate method for working in navController
-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
I really think that the correct way to do it is to set both landscape and portrait as supported orientations, and not allowing a change in the orientation on the VC you don't want to rotate, by returning NO on the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
if the orientation that was passed to the method was not supposed to be supported.
As mentioned in the comments, this method can no longer be used. You should use :
-(BOOL) shouldAutorotate
And find out the current orientation inside this function and returning NO if you don't want to rotate.
Don't forget to allow your application different Orientations!
otherwise nothing from above will work
Set it in project target on General under Deployment Info section:

Understanding iOS 6 Interface orientation change

ADDED:
I see that my question is viewed often without upvotes so I decided that you guys do not get what you search. Redirecting you to question that has really nice answer about
How to handle orientation changes in iOS6
Specific demands to orientation changes:
Restricted rotation
Upvotes are welcome :)
I've created a new project from Master Detail template and trying to start it with landscape orientation.
As you know the
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method is deprecated and we must use
- (NSUInteger)supportedInterfaceOrientations
and/or
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
Here's my code:
- (NSUInteger)supportedInterfaceOrientations {
NSLog(#"supported called");
return UIInterfaceOrientationMaskAll;//Which is actually a default value
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(#" preferred called");//This method is never called. WHY?
return UIInterfaceOrientationLandscapeRight;
}
As you can see I'm trying to return landscape orientation in preferred method but it is never called.
p.s. documentation states:
Discussion The system calls this method when presenting the view
controller full screen. You implement this method when your view
controller supports two or more orientations but the content appears
best in one of those orientations.
If your view controller implements this method, then when presented,
its view is shown in the preferred orientation (although it can later
be rotated to another supported rotation). If you do not implement
this method, the system presents the view controller using the current
orientation of the status bar.
So, the question is: Why the prefferredOrientation method is never get called? And how should we handle different orientations in different controllers?. Thanks!
P.S don't mark the question as duplicate. I've investigated all similar questions and they do not have answer for mine.
About preferredInterfaceOrientationForPresentation
preferredInterfaceOrientationForPresentation is never called because this is not a "presented" view controller. There is no "presentation" involved here.
"Presented" and "presentation" are not some vague terms meaning "appears". These are precise, technical terms meaning that this view controller is brought into play with the call presentViewController:animated:completion:. In other words, this event arrives only if this is what we used to call a "modal" view controller.
Well, your view controller is not a modal view controller; it is not brought into play with presentViewController:animated:completion:. So there is no "presentation" involved, and therefore preferredInterfaceOrientationForPresentation is irrelevant here.
I'm being very explicit about this because I'm thinking that many folks will be confused or misled in the same way you were. So perhaps this note will help them.
Launch into Landscape
In iOS 6, the "Supported Interface Orientations" key in your Info.plist is taken much more seriously than previously. The solution to your overall problem of launching into a desired orientation is:
Make sure that "Supported Interface Orientations" in your Info.plist lists all orientations your app will ever be allowed to assume.
Make sure that the desired launch orientation is first within the "Supported Interface Orientations".
That's all there is to it. You should not in fact have to put any code into the root view controller to manage the initial orientation.
If you would like launch modal view in Landscape Mode, just put this code in presented view controller
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIInterfaceOrientation orient = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsLandscape(orient)) {
return orient;
}
return UIInterfaceOrientationLandscapeLeft;
}
Then, present this controller as usual
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self.navigationController presentViewController:vc animated:YES completion:^{}];
There is a very simple answer: You can only change or fix the interface orientation of a modal presented view controller. If you do so i.e. with a Present Modally segue in Interface builder (or the navigation controller method) you can define the allowed orientations with
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait; // for example else an addition of all allowed
}
This event doesn't fire up when you only push a view controller on the navigation Controller ... so : You don't have a BACK button and need a
[self dismissViewControllerAnimated:YES completion: ^(void) {
}];
to close it.

Why does this orientation code not work?

I have a simple UIWebView nested in a ViewController it is presented modally off a navigation controller.
Structure as follows: TableViewController -> TableViewController -> Modal (WebView presented) -> Modal (options, presented via gesture, page curl)
All views handle all orientations except the one with the WebView on it, it is to handle its orientation via configuration.
The controller with the web view has the following orientation code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL orientationSwitch = NO;
if([self.webApp.orientationLandscape boolValue] &&
UIInterfaceOrientationIsLandscape(interfaceOrientation))
orientationSwitch = YES;
if([self.webApp.orientationPortrait boolValue] &&
UIInterfaceOrientationIsPortrait(interfaceOrientation))
orientationSwitch = YES;
return orientationSwitch;
}
Now the intention is that you can set some options as to if the view should react to the orientation changes. And for the most part it works, however I have a modal popup where you can choose to dismiss the modal that contains the web view. Once this happens, it seems the orientation is locked for the entire app. All other views subsequently do not respond to any form of rotation.
Any ideas?
Thanks
Here's something to try. Register with the UIDevice for UIDeviceOrientationDidChangeNotification and tell it to start generating notifications. When a notification comes in, call the UIViewController class method attemptRotationToDeviceOrientation (iOS 5 only).
Also it might help to read this tech note, as you may be doing something wrong here: https://developer.apple.com/library/ios/#qa/qa1688/_index.html
first, make sure in your project settings are the right supported orientations clicked (targets -> appname ->summary)
I don't know why this is happening, but I had a similar issue quite a while ago. That long time ago, ios 3.2 or something. I created a ViewRotatorController which only implements the -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{}
method. Every other ViewControllers inherits that. Just let your webappview has a other implementation of this. It is probably not the best solution but it should work.

UIViewController orientations

I have a UIViewController instance(viewController1) that I want to lock in landscape mode which I am able to do. On tapping on a button I push another UIViewController(viewController2) which supports both orientations. Now, if user changes the orientation of viewController2 to portrait and goes back to viewController1, viewController1 also changes it's orientation to portrait. How can I avoid that?
Thanks in advance.
Add these methods to the view controllers
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Thats for the first
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES);
}
Thats the second
-(BOOL)shouldAutorotateToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
return YES;
}
return NO;
}
If those 2 controllers are both implementation of UIViewController both differente classes each other! you can just implement the method shouldAutorotateToInterfaceOrientation, in the first! this should work even when u go back!
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
return YES;
}
return NO;}
You say push so I assume both ViewControllers are in a NavigationController. If so I'll have to disappoint you, what you want isn't possible. Your rotation callbacks are working correctly, they respond to a rotation, you can't force it. What's happening is the correct behavior.
Best solution is to prevent the user from going back when you're in the orientation the previous ViewController doesn't support, hide the back button for example.
A while back I've made my own NavigationController (doesn't inherit from the UIViewController but it can do exactly the same) and I've tried to implement what you're trying to do. Before pushing or popping, if the view of the ViewController that was about to be shown didn't support the current orientation, I transformed the view of that ViewController by 90° and force the orientation of the status bar to the new ViewController's supported orientation.
As soon as the push or pop was complete I'd do a small trick to force the rotation of the device. If you remove the view of the rootViewController from the window and re-add it, the responder chain will be forced to go through all rotation callbacks. When that happened I checked if a ViewController's view was transformed and reset that transformation.
It did work, mostly. But it was messy code and it goes against Apple's current policy that the rootViewController should be responsible of handling the orientation. Also in iOS6 forcing the status bar orientation is guaranteed to work. So I'd really advise against doing this, I've removed this from my own NavigationController too.