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

Rohan picture Rohan · Jul 4, 2013 · Viewed 66.9k times · Source

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

Alan10977 picture Alan10977 · Sep 20, 2014

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