UIDeviceOrientationDidChangeNotification is return

2019-09-02 00:43发布

问题:

I am registering my viewcontroller for UIDeviceOrientationDidChangeNotification and it's return wrong orientation of device. I am registering it in init function of viewcontroller

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleOrientationChangeNotification:) name: UIDeviceOrientationDidChangeNotification object: nil];

This is my device Notification Receiver method.

-(void)handleOrientationChangeNotification:(NSNotification *)notification
{ 
    if(IS_IPHONE)
    {
        UIDeviceOrientation currentDeviceOrientation =  [[UIDevice currentDevice] orientation];
    .
    .
    .
}

Whenever Device Orientation changed,I am always getting wrong orientation.

回答1:

I find the solution on apple site on this link

UIDeviceOrientationLandscapeRight is assigned to UIInterfaceOrientationLandscapeLeft and UIDeviceOrientationLandscapeLeft is assigned to UIInterfaceOrientationLandscapeRight. the reason for this is that rotating the device requires rotating the content in the opposite direction.



回答2:

This is Probably happening because Apple Has changed the Way of managing the Orientation of UIViewController. In Ios6 Oreintation handles Differently, in iOS6 shouldAutorotateToInterfaceOrientation method has deprecated.iOS 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.

For More Information regarding the same You should visit this link Below I have made the Category for handling the Orientation Change.

SO You will have to implement the Two more methods for managing the Orientation of UIViewController in iOS6.

Introduced In IOS6 Allow Orientation Change

 - (BOOL)shouldAutorotate
  {

    return YES;

  }

Return the Number of Oreintation going to supported in device

 - (NSUInteger)supportedInterfaceOrientations
 {
    return  UIInterfaceOrientationMaskAll;

 }

Now check what orientation you are getting.

EDIT: Place this Code to your FirstViewController added as root ViewController .this will help the UIViewController to determine it's Orientation.

@implementation UINavigationController (RotationIn_IOS6)

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

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

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

 @end

I hope i'll be helpful to you.