My goal is changing lock/unlock orientation using button. In lock mode, orientation can be switched by button, and in unlock mode, orientation is determined by sensor.
I tried to achieve this with [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:orientation] forKey:@"orientation"]
, but this code has problem.
Assume that currently my app is 'Lock mode, portrait UI, portrait device'. Then I rotate device to landscape left, and unlock orientation. I expected [[UIDevice currentDevice] orientation]
should be UIDeviceOrientationLandscapeLeft
. But the value was UIDeviceOrientationPortrait
though the real device is in landscape!
I tried also notification center with code below.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(onDeviceOrientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
But this code does not working properly. If the device is in landscape and UI orientation is set to portrait by [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationPortrait]
, onDeviceOrientationChanged
is not called when I rotate device to portrait. I think device's orientation value is already set to portrait (by my code, not sensor).
ps. Of course, I checked Required full screen option.
EDIT: It would be also good answer to know how to get device's real orientation which is not affected by [[UIDevice currentDevice] setValue]
.