早在 2010 年,How to prevent a modal UIImagePickerController from rotating? 对此问题没有找到满意的答案。
我找到了一个替代方案,但这也像是让你被 App Store 拒绝:从窗口中删除所有观察者。
如果您的应用程序在 viewController 之外的某些部分需要方向更改消息,它可以注册它们:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
毫无疑问,窗口会注册此类通知,然后在视图控制器接受新方向时旋转它们。它不方便查看UIDeviceOrientationDidChangeNotification,因此必须有一个私人命名的等价物。您所能做的就是从窗口中删除每个通知:
[[NSNotificationCenter defaultCenter] removeObserver:[[self view] window]];
如果您只是想暂时抑制方向更改,则无法恢复。我们也无法判断窗口是否正在监视来自其他来源的任何重要通知。也是不完整的——翻身按钮,拍照按钮,自行移动和旋转。
你如何解决这个怪癖?您可以关闭 UIImagePicker 上的 showsCameraControls,并提供您自己的 cameraOverlayView 以及上面的相机按钮。或者通过遍历整个视图层次结构(也是拒绝材料)继续随机删除通知,从所有内容中删除通知:
- (void) removeAllNotificationsFromView:(UIView*) view;
{
// This recurses through the view hierarchy removing all notifications for everything.
// This is potentially destructive, and may result in rejection from the App Store.
[[NSNotificationCenter defaultCenter] removeObserver:view];
for (UIView *subview in [view subviews])
[self removeAllNotificationsFromView:subview];
}
[self removeAllNotificationsFromView:imagePicker.view];
我不建议使用它,但它提供了一些关于如何检测和传播方向变化的有趣见解。