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

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

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..


Your view controller will never rotate to any position that is not supported by the app itself. You should enable all possible rotations and then in view controllers that are not supposed to rotate put the following lines

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

In ChatView, it should be:

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

If you need to change your layout after a rotation you should implement the appropriate changes to your subviews in

- (void)viewWillLayoutSubviews

Use self.view.bounds to check the current size of the view, since self.view.frame doesn't change after rotations.