在这里回答为时已晚,但我正在扩展@Adam Woś 的答案,
在介绍之前UIImagePickerController
,
UIImagePickerController *controllerObject = [[UIImagePickerController alloc] init];
...
...
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)name:UIDeviceOrientationDidChangeNotification object:nil];
...
...
[self presentViewController:controllerObject animated:YES completion:nil];
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]];
}
- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(@".....landscape.....");
}
else if(orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
{
NSLog(@".....portrait.....");
}
}
当UIImagePickerController
被解雇时不要忘记,
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
另请注意,根据@FelixLam 对@Adam Woś 回答的评论,
如果设备方向被锁定,则不再发布通知。
为了处理这种情况,需要实现CoreMotion
(替代UIAccelerometer
iOS < 6.0)来检测设备方向是否被锁定。UIAccelerometer
这是http://blog.sallarp.com/iphone-accelerometer-device-orientation/的博客,这是针对CoreMotion
https://github.com/tastyone/MotionOrientation的博客,您必须使用一些逻辑来检查这一点。
祝你好运!