Rohan Rohan - 5 months ago 73
Objective-C Question

How to allow only single UIViewController to rotate in both Landscape and Portrait direction?

My app is only for

iphone
device (both iphone 4 and 5) and built to support only
ios 6
.

My whole app only supports
portrait
mode. But there is one view called "
ChatView
" , which i want to support both
landscape
and
portrait
modes.

I have set the required device rotations as follows -

enter image description here

I have also tried following code to support rotation in "ChatView" -

-(BOOL)shouldAutorotate
{
return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}


But it could not rotate that view.

I have searched a lot for this , but could not be able to find the solution for my issue.

And also in "ChatView" there are some objects like buttons, textfields whose frames are set programmaticaly. So i want to know should i have to set frames of all those objects for landscape mode also?

Please help me.

Thanks.....

Answer

I think if you want to support just one viewcontroller rotation, it is not possible since application will follow orientations set by you in .plist file. An alternate you can follow is to support your app for both landscape and portrait, freeze all viewcontrollers rotation to portrait except for chat view.

EDIT

To subclass UINavigationController, create a new file with name e.g. CustomNavigationController and make it subclass of UINavigationController.

.h file

#import <UIKit/UIKit.h>

@interface CustomNavigationController : UINavigationController

@end

.m file

#import "CustomNavigationController.h"

@interface CustomNavigationController ()

@end


@implementation CustomNavigationController

-(BOOL)shouldAutorotate
{
    return NO;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


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

@end

Set the class of your UINavigationController in your main class xib as CustomNavigationController. Hope it helps ypu..