Orientation issue in ios 6

2020-01-29 22:40发布

问题:

I am working in universal app which works fine till ios 5 but in ios 6 there is orientation problem. My app is only in portrait mode. The problem is when my ipad is in landscape mode and than if i start my app than the orientation does nt change to protrait. So please help me with some code so that i force my app to be in protrait mode only independent of the orientation of device before the app launches

回答1:

This is happening because Apple Has changed the Way of managing the Orientation of UIViewController. In Ios6 Oreintation handles Differently .in iOS6 containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.So device getting orientation changed by default.

So Need To Perform Some Steps.

1 -Set the Orientation From Here

2 -Place This Code in FirstViewController added to the RootViewController

  @implementation UINavigationController (RotationIn_IOS6)
   //Here UINavigationController is the RootViewController added To the Window

 -(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}

 -(NSUInteger)supportedInterfaceOrientations
{
   return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
     return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end

** Put below methods in ViewController,Introduced In IOS6 Allow Oreintation Change**

  - (BOOL)shouldAutorotate
 {

   return NO;

  }

/*Return the Number of Oreintation going to supported in device*/

 - (NSUInteger)supportedInterfaceOrientations
  {
return  (UIInterfaceOrientationMaskPortrait |     UIInterfaceOrientationMaskPortraitUpsideDown );

 }

 // Returns interface orientation masks.

  - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  {
      return  (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown      ) ;

 }


回答2:

First we have to set supported orientations in Targets->Summary...

In iOS6.0 shouldAutorotateToInterfaceOrientation Method is deprecated.So,instead of this method we have to use shouldAutorotate Method..

And with the method supportedInterfaceOrientations we have to set which orientations we want like UIInterfaceOrientationMaskAll if we want all orientations



回答3:

In your ViewController.m you will have to define your supported orientation. In IOS 6 this is the method which handle the supported orientation.

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}

UIInterfaceOrientationMaskAll means it will support all orientation of view.

Also read the Apple Doc - Doc about Orientation



标签: iphone ios6