所以答案不干净,但我可以给你一个修复。在某些时候,主应用程序窗口不能像现在那样自动旋转,但在某些时候它开始根据 rootviewcontroller 旋转。至少这个源代码表明了这一点。我在 iOS 6 结束时开始开发,我认为这个源代码是关于那个时候写的。对于我来说,允许在示例中旋转但预览不旋转的最佳解决方法是添加第二个窗口。将主窗口背景设置为清除。然后将 previewLayer 添加到主窗口后面的第二个窗口。在代码中它看起来像这样。
AppDelegate 看起来像这样。
#import <UIKit/UIKit.h>
#import <CoreMotion/CoreMotion.h>
@interface FHAppDelegate : UIResponder <UIApplicationDelegate>
{
@private
CMMotionManager *_motionManager;
}
@property (strong, readonly, nonatomic) CMMotionManager *motionManager;
//future preview window
@property (strong, nonatomic) UIWindow *previewWindow;
@property (assign, readonly, nonatomic) UIDeviceOrientation realDeviceOrientation;
@end
然后在 FHViewController 的 viewDidLoad 中而不是添加到主窗口中,我这样做了,它添加了他们获得主窗口的位置,我将 previewView 添加到那里。
// we make our video preview view a subview of the window, and send it to the back; this makes FHViewController's view (and its UI elements) on top of the video preview, and also makes video preview unaffected by device rotation
//nothing special about this viewcontorller except it has
//-(BOOL)shouldAutorotate{
//return NO;
//}
TestViewController *test = [[TestViewController alloc]initWithNibName:@"TestViewController" bundle:nil];
FHAppDelegate *delegate = ((FHAppDelegate *)[UIApplication sharedApplication].delegate);
delegate.previewWindow = [[UIWindow alloc]initWithFrame:window.bounds];
UIWindow *previewWindow = delegate.previewWindow;
[window setBackgroundColor:[UIColor clearColor]];
previewWindow.rootViewController = test;
previewWindow.windowLevel = UIWindowLevelNormal - 1;
[previewWindow setBounds:delegate.window.bounds];
[previewWindow makeKeyAndVisible];
[previewWindow addSubview:_videoPreviewView];
[previewWindow sendSubviewToBack:_videoPreviewView];
因为 previewWindow 必须有一个 rootviewcontroller 并且它决定了旋转,所以你可以看到我的 testviewcontroller 的自动旋转为 NO。希望这可以帮助。它在 iOS 10 上为我工作。
编辑:上面示例中的视图不旋转,但旋转时的窗口动画在视觉上很糟糕。它可以通过覆盖来删除
willTransitionToSize
[UIView setAnimationsEnabled:NO];
完成后
[UIView setAnimationsEnabled:YES];
在GitHub上查看快速版本
对于 iPad,必须选中全屏。