Wednesday 13 May 2015

Presenting Landscape View in Portrait Orientation iOS app

We develop lots of application where there is the requirement of opening a particular screen in the landscape mode while all the other screens are in the portrait mode. So how can we achieve this. Could we have to do transformation to rotate it. The answer is simple no. Why should we do transformation when there is a better way to do that. In next 15 minutes you will be able learn to do it.

To do this we have to create a Custom Navigation Controller that supports only Landscape View.

1) Create a .h file and declare a class that Inherits UINavigationController.

@interface CustomNavigationControllerViewController : UINavigationController

@end

2) Now create a .m file and declare the supported interface orientation in to be Landscape in this.

@implementation CustomNavigationControllerViewController

- (NSUInteger)supportedInterfaceOrientations
{
         return UIInterfaceOrientationMaskLandscape;
}

Now you are done the only thing remain is to setup your project.

3) Create a UIViewController that is to be presented in the landscape mode.

4) Now simply create a CustomNavigationControllerViewController class object with root view as UIViewController that is to be presented in the landscape view.

LandscapeView *landscape = [[LandscapeView alloc]initWithNibName:@"LandscapeView" bundle:nil];
CustomNavigationControllerViewController *nav = [[CustomNavigationControllerViewController alloc]initWithRootViewController:landscape];
nav.navigationBarHidden = true;
[self presentViewController:nav animated:YES completion:^{
}];


5) Now present the CustomNavigationControllerViewController inside the portrait screen. It should keep in the mind to disable the orientation and restrict the portrait mode.




Here is sample project with all of the code from the above tutorial.



2 comments: