从横向启动时,UIViewController 在 AirPlay 屏幕上横向显示 iPad

UIViewController displayed sideways on AirPlay screen when launched from landscape iPad

我正在尝试使用 AirPlay 在外部显示器上显示全屏,16:9,UIViewController

这里的目标是用覆盖整个外部屏幕的自定义视图控制器替换 AirPlay 镜像。

当 iPad 处于纵向模式时连接屏幕时,一切似乎都很好。当它横向连接时,UIViewController 在外部显示器上显示在侧面并且只占满屏幕的一半。

为此,我将 UIViewController 添加到附加的 AirPlay UIScreen

-(void) screenConnected:(NSNotification * ) notification {

    UIScreen * screen = [notification object]; //this should be the airplay display
    UIWindow * window = [[UIWindow alloc] initWithFrame:screen.bounds];
    [currentWindow setScreen:screen];
    UIViewController * controller = [_delegate createControllerForAirplayDisplay:window];
    [window setHidden:NO];
    [window setRootViewController:controller];

}

这似乎工作正常,我在 iPad 和 AirPlay TV 上看到全屏显示。

我看到的问题是当 iPad 处于横向模式时连接 AirPlay 显示器。发生这种情况时,AirPlay 显示会在侧面呈现 UIViewController 并且看起来像纵向模式。

纵向连接的屏幕:

横向连接的屏幕,内容是横向的,只在一半的显示器上呈现:

我尝试使用 CGAffineTransformMakeRotation 旋转 UIWindowUIViewController,这确实修复了方向,但视图没有在 AirPlay 显示中居中。

编辑

向 Apple 提交了与此相关的问题,rdar://20817189。我会更新这个 if/when 我听到了。

始终使用 shouldAutorotateToInterfaceOrientation 方法 return YES 应该会为您解决问题。

Airplay windows 设置为肖像蒙版永远不会旋转。默认状态栏为纵向,因此旋转值基于此。这是一个有效的代码示例(假设您在 AppDelegate 连接时将 Airplay UIWindow 存储为 self.secondWindow)

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if(window == self.secondWindow){
        return UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskAll;
}

我在文档中找到了这个: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/UsingExternalDisplay/UsingExternalDisplay.html

Important: Be sure that your app sets the status bar orientation correctly. Because the external display can’t read the host device’s accelerometer to respond to changes in orientation, it relies instead on the status bar orientation, as set in your app.

外部 UIWindow 采用当前设备的方向,在本例中为横向。 因此,当您设置 rootViewController 的框架时,例如 (0, 0, 1270, 840),原点位于电视的右上角。

您必须强制 UIWindow 纵向。 我在 AppDelegate:

上用这段代码解决了这个问题
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)_window {
if ([UIScreen mainScreen] == _window.screen || !_window) {
    return UIInterfaceOrientationMaskAll;
}else {
    return UIInterfaceOrientationPortrait;
}
}