在iOS8.0(和8.1)上使用第二个UIScreen自动调整问题

Sco*_*tty 2 uiwindow ios uiscreen

我的应用程序驱动第二个屏幕(外部显示器),但我看到一些关于旋转的"奇怪"事情(在iOS7上没有发生的事情)

如果我以横向方向启动应用程序(并连接第二个屏幕),然后按主页按钮将应用程序放入后台,然后重新打开应用程序,然后将第二个屏幕(连接到显示器)旋转90度并仅使用一半的屏幕.没有任何后续旋转修复此问题.

我非常有信心这是一个错误 - 但我很乐意另外知道.下面是在简单的单视图应用程序中重现它的代码.

谢谢

@interface AppDelegate ()

@property (nonatomic, strong) UIWindow* externalWindow;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidConnect:) name:UIScreenDidConnectNotification object:nil];

    UIScreen* externalScreen = ([UIScreen screens].count > 1 ? [[UIScreen screens] objectAtIndex:1] : nil);
    if (externalScreen)
    {
        [self setupExternalScreen:externalScreen];
    }

    return YES;
}

- (void) screenDidConnect:(NSNotification *)aNotification
{
    UIScreen* externalScreen = (UIScreen*)aNotification.object;
    [self setupExternalScreen:externalScreen];
}

- (void)setupExternalScreen:(UIScreen*)externalScreen
{
    externalScreen.currentMode = externalScreen.preferredMode;

    self.externalWindow = [[UIWindow alloc] initWithFrame:externalScreen.bounds];
    self.externalWindow.screen = externalScreen;
    self.externalWindow.clipsToBounds = YES;
    self.externalWindow.hidden = NO;
    [self.externalWindow makeKeyAndVisible];

    UIViewController* externalViewController = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    externalViewController.view.backgroundColor = [UIColor redColor];
    self.externalWindow.rootViewController = externalViewController;
}
@end
Run Code Online (Sandbox Code Playgroud)

Sco*_*tty 5

好的 - 修好了.

而不是设置

self.externalWindow.rootViewController = externalViewController;
Run Code Online (Sandbox Code Playgroud)

相反,只需将视图添加为窗口的子视图(记住在视图控制器对象上保留引用)

self.externalViewController.view.frame = self.externalWindow.frame;
[self.externalWindow addSubview:self.externalViewController.view];
Run Code Online (Sandbox Code Playgroud)

我猜视图控制器的东西很混乱.....