UIDeviceOrientationDidChangeNotification is returning UIOrientationLandscapeLeft for UIOrientationLandscapeRight

不问归期 提交于 2019-12-12 04:33:18

问题


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.



来源:https://stackoverflow.com/questions/12889991/uideviceorientationdidchangenotification-is-returning-uiorientationlandscapeleft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!