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

时间:2014-10-16 15:18:12

标签: ios uiwindow 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

3 个答案:

答案 0 :(得分:5)

好的 - 修好了。

而不是设置

self.externalWindow.rootViewController = externalViewController;

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

self.externalViewController.view.frame = self.externalWindow.frame;
[self.externalWindow addSubview:self.externalViewController.view];

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

答案 1 :(得分:0)

另一个似乎有效的解决方案:在窗口的根视图控制器中覆盖supportedInterfaceOrientationsshouldAutorotate

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

答案 2 :(得分:0)

我只是转换UIWindow来解决它

CGRect frame = screen.bounds;

if (!self.secondWindow) {
    UIWindow *extWindow = [[UIWindow alloc] initWithFrame:frame];
    self.secondWindow = extWindow;
}

if ([[[UIDevice currentDevice] systemVersion] integerValue] == 8) {
    CGFloat magicAmount = (frame.size.width - frame.size.height) / 2;
    if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
        float rotation = M_PI_2;
        self.secondWindow.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rotation), magicAmount, magicAmount);
    }
    else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
        float rotation = -M_PI_2;
        self.secondWindow.transform = CGAffineTransformTranslate(CGAffineTransformMakeRotation(rotation), -magicAmount, -magicAmount);
    }
}