How to Make App Fully Working Correctly for Autorotation in iOS 6

How to make app fully working correctly for autorotation in iOS 6?

Figured it out.

1) subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.)
did set it as self.window.rootViewController.

- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}

2) if you don't want view controller rotate

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

3) if you want it to be able to rotate

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
return YES;
}

BTW , According to your needs ,another related method :

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}

Autorotate in iOS 6 has strange behaviour

From Apple's iOS 6 SDK Release Notes:

Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow: and shouldAutorotate methods.

More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.

A view controller’s supported interface orientations can change over time—even an app’s supported interface orientations can change over time. The system asks the top-most full-screen view controller (typically the root view controller) for its supported interface orientations whenever the device rotates or whenever a view controller is presented with the full-screen modal presentation style. Moreover, the supported orientations are retrieved only if this view controller returns YES from its shouldAutorotate method. The system intersects the view controller’s supported orientations with the app’s supported orientations (as determined by the Info.plist file or the app delegate’s application:supportedInterfaceOrientationsForWindow: method) to determine whether to rotate.

The system determines whether an orientation is supported by intersecting the value returned by the app’s supportedInterfaceOrientationsForWindow: method with the value returned by the supportedInterfaceOrientations method of the top-most full-screen controller.
The setStatusBarOrientation:animated: method is not deprecated outright. It now works only if the supportedInterfaceOrientations method of the top-most full-screen view controller returns 0. This makes the caller responsible for ensuring that the status bar orientation is consistent.

For compatibility, view controllers that still implement the shouldAutorotateToInterfaceOrientation: method do not get the new autorotation behaviors. (In other words, they do not fall back to using the app, app delegate, or Info.plist file to determine the supported orientations.) Instead, the shouldAutorotateToInterfaceOrientation: method is used to synthesize the information that would be returned by the supportedInterfaceOrientations method.

If you want your whole app to rotate then you should set your Info.plist to support all orientations. Now if you want a specific view to be portrait only you will have to do some sort of subclass and override the autorotation methods to return portrait only. I have an example here:

https://stackoverflow.com/a/12522119/1575017

Rotation behaving differently on iOS6

The most important part of the documentation I found for this issue is:

When the user changes the device orientation, the system calls this
method on the root view controller or the topmost presented view
controller that fills the window

To make my app fully working for autorotation in iOS 6, I had to do the following:

1) I created a new subclass of UINavigationController, and added shouldAutorotate and supportedInterfaceOrientation methods:

// MyNavigationController.h:
#import

@interface MyNavigationController : UINavigationController

@end

// MyNavigationController.m:
#import "MyNavigationController.h"
@implementation MyNavigationController
...
- (BOOL)shouldAutorotate {
return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
...
@end

2) In the AppDelegate, I did use my new subclass to show my root ViewController (it is introScreenViewController, a UIViewController subclass) and did set the self.window.rootViewController, so it looks that:

    nvc = [[MyNavigationController alloc] initWithRootViewController:introScreenViewController];
nvc.navigationBarHidden = YES;
self.window.rootViewController = nvc;
[window addSubview:nvc.view];
[window makeKeyAndVisible];

Autorotation iOS6

You do not need to set the interface orientation in your App Delegate object. Select in the Project Navigator your Project File. Then select the Target and select Summery. Here you can select the Supported Interface Orientations. This works for the whole project.

auto-rotation in iOS5/6?

that is because there was never a success. You should choose one of the orientations.

Hold command and click on UIInterfaceOrientation you will see an enumeration of the possible options.

then you can test against those to decide your YES Scenario.

I may have originally misunderstood your problem. It seems that you may have been saying that your app is allowing rotation. but the code should disallow that.

I was thinking you were saying it was still firing the code. Trying to find a Yes

One thing to think about. is there may be more than one view controller available. perhaps your code is not being hit.

A couple of possible issues for this.

  1. Your code is not even being used. because the view is being allocated as UIViewController as opposed to your custom view controller.

  2. You code is being used but that View controller is not the one being asked about the Orientation. therefore that specific code is not being hit.

  3. A bad build keeps putting the wrong assemblies onto the device.

Your solutions can be as follows.

  1. Ensure your code is the one being allocated. Either there is a direct alloc on your custom class. or the xib file is inflating it. Check out the Identity Inspector when you have your xib file open. select your View Controller and ensure that custom class is set to your class type

  2. Look at the hierarchy. what other view controllers are there. Perhaps one of those are telling the app it can autorotate to any orientation.

  3. Find your "DerivedData" folder and remove it entirely. Sometimes this works from the organizer. other times you need to delete directly off the disk. Then clean and build again.

Also another solution could be as simple as setting the settings in the Project file.

Select your project file from the file browser and you will see the iPad and iPod settings in the summary. You can "UnPress" buttons for the orientations that you want to disallow. and any view controllers that you do not otherwise code orientation into. will use these by default.

My apologies for the confusion.

Update

I commonly use this code to handle my autorotation.

It not only differentiates the ipad from the other ios devices, but it also forwards the request onto presented controllers so a view that is shown modal may respond how it wants.

Orientation is a pain when you dont understand it :)

// Detect iPad
#define IS_IPAD() ([[UIDevice currentDevice] respondsToSelector:@selector(userInterfaceIdiom)] ? \
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad : NO)

// Set preferred orientation for initial display
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
if (IS_IPAD()){
return UIInterfaceOrientationLandscapeRight;
}
else {
return UIInterfaceOrientationPortrait;
}
}
// Return list of supported orientations.
- (NSUInteger)supportedInterfaceOrientations{
if (self.presentedViewController != nil){
return [self.presentedViewController supportedInterfaceOrientations];
}
else {
if (IS_IPAD()){
return UIInterfaceOrientationMaskLandscapeRight;
}
else {
return UIInterfaceOrientationMaskAll;
}
}
}

// Determine iOS 6 Autorotation.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// Return yes to allow the device to load initially.
if (orientation == UIDeviceOrientationUnknown) return YES;
// Pass iOS 6 Request for orientation on to iOS 5 code. (backwards compatible)
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
// handle iOS 5 Orientation as normal
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations

if (self.presentedViewController != nil){
return [self.presentedViewController shouldAutorotate];
}
else {
if (IS_IPAD()){
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
}

iOS6: supportedInterfaceOrientations not working (is invoked but the interface still rotates)

If your are using a UINavigationController as the root window controller, it will be its shouldAutorotate & supportedInterfaceOrientations which would be called.

Idem if you are using a UITabBarController, and so on.

So the thing to do is to subclass your navigation/tabbar controller and override its shouldAutorotate & supportedInterfaceOrientations methods.



Related Topics



Leave a reply



Submit