Orientation don't change in second time

420 Views Asked by At

i have this code below, that recognize when the device change his orientation, and if the orientation are landscape left or right, he back again to portrait:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(orientationChanged:)
     name:UIDeviceOrientationDidChangeNotification
     object:[UIDevice currentDevice]];


- (void) orientationChanged:(NSNotification *)note{

    UIDevice * device = note.object;

    if(device.orientation == UIDeviceOrientationLandscapeRight){


        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
    }

    if(device.orientation == UIDeviceOrientationLandscapeLeft){


        [[UIDevice currentDevice] setValue:
         [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
                                    forKey:@"orientation"];
    }

}

When I run my project and change the orientation of my device to landscape right or left, this code recognize and change the orientation to portrait, but if I rotate my device again to landscape left or right, the code don't work and my device stay in landscape mode, why? and how to solve this problem?

3

There are 3 best solutions below

0
iAnurag On

I dont know if I am correct but try to fire your notification from viewWillAppear

 -(void)viewWillDisappear:(BOOL)animated
{
  [super viewWillAppear] ;
   [[NSNotificationCenter defaultCenter]
 addObserver:self selector:@selector(orientationChanged:)
 name:UIDeviceOrientationDidChangeNotification
 object:[UIDevice currentDevice]];

}
0
Alex Andrews On

It is better practice to register the notifications in the viewWillAppear and unregister in the viewWillDisappear method. So that you will catch the notifications only if your view is presenting.

Implement like :

-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear] ;
  [[NSNotificationCenter defaultCenter]
     addObserver:self selector:@selector(orientationChanged:)
     name:UIDeviceOrientationDidChangeNotification
     object:[UIDevice currentDevice]];
}

-(void)viewWillDisappear:(BOOL)animated
{
  // Removing observer for lead created notification
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

And if UIDeviceOrientationDidChangeNotification is not solving your problem, try StatusBarOrientationNotification like as follows.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusBarOrientationChange:(NSNotification *)note {
    UIInterfaceOrientation orientation = [note.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];

    // handle changes
}
0
pronebird On

Instead of messing with private API and get rejected from AppStore, you should simply lock rotation for your controller using standard methods such as supportedInterfaceOrientations and shouldAutorotate.