RSS Feed

iOS 6 : How to fix rotation for different UIViewController Class

iOS seems awkward for rotation of iPhone/iPad app screen. Sometimes you need to rotate only single view controller class without rotating any other screen. In XCode if you check App Target (support interface orientations) there are four options are given :
1. Portrait 2. Upside down 3. Landscape left 4. Landscape right.

If you select only 1 and 2, you app will work only in portrait mode. Your rotation code will not affect on iPhone screen. Same for Landscape.

Lets consider my app in which i have 2 screens. Both are inherited from UIViewController class. My first screen (ViewController1) should run only in Portrait mode and my second screen (ViewController1) may work in all orientation (i.e portrait and landscape). Follow these step for fix the such case in your app :

1. In your app delegate make sure you are not using [self.window addSubview:RootController.view];
You should use : [self.window setRootViewController:RootController];

2. Inside App Target select all orientation
Orientaitons

3. In your class ViewController1.m write this code : // This class will work only in Portrait.
– (BOOL)shouldAutorotate {

return NO;
}
– (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

4. In your class ViewController2.m write this code : // This class will work in all orientations.
– (BOOL)shouldAutorotate{

return YES;
}
-(NSUInteger)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskAll;
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

if ([self.navigationController.visibleViewController isKindOfClass:[ViewController2 class]])
return (YES);
else
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

Now run the app. It works like charms !!

About these ads

About Muzammil

Over the past 4 years, I have designed, developed and managed over 60 iPhone/iPad applications for many large-scale clients, some include Rolex, Ford, Makemark LLC, Source Digital Production, IT City (Kingdom of Saudi Arabia), R.Ø.S.A. Creation etc. These applications ranged from simple navigation bar application to high end user interface design for image processing, simple games with Core Graphics and OpenGL. Before started Triffort Technologies i was working in Ampere Software Pvt Ltd as a senior iOS Developer. I was the first employe of Ampere who started iPhone/iPad development in the company. In ampere i worked on some complex apps like Boxboss, Austin Post, User Tracking etc. Throughout my career, I have used Core Graphics, OpenGL, SQLite, Core Data, JSON, XML etc. I am very proficient in many design programs including Photoshop, Illustrator, Flash, iMove. Although I am very knowledgeable in all Adobe Products. Most the time i am designing my apps without taking help from any graphics designer. I love to make app from my designs. It feels great when you are creating app from scratch and no body else is involve in your ideas. You are free to do anything. As the Senior Developer and Team lead at Triffort Technologies, I managed a team of Technical Interactive Developers and Designers. As their manager I am responsible for scheduling, resource management, task reviews, process development and enhancements, employee reviews and new employee hiring. I believe good employee morale and communication are key factors to successful, creative work, therefore, positive feedback was part of my daily routine. But most of the time you can find me on my desk writing codes for some app. I love programming. This is my passion. I’m a team player. I am a motivated individual who enjoys taking on new challenges and learning new technologies. I am well organized and a proficient multi-tasker. I have strong problem solving skills such as the ability to use technology in creative ways to meet unique project requirements. I love to be challenged and I always have a positive outlook no matter how tight the deadline is. If you would like to contact me or find out more about my experience, please contact me at muzammil@triffort.com or you can Skype me at muz.iphonedev.

4 responses »

  1. Great! You save my day :) The first you mention helped me well! Thanks!

    Reply
  2. Hey! Thank you for the tip! it works.

    Reply
  3. Great Blog !!!!..Really happy to find it…:)

    Reply
  4. Thank you Waqaar. For latest updates please use this blog url : http://blogs.triffort.com

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 68 other followers

%d bloggers like this: