Xcode 6 iOS 8 旋转问题 - 屏幕上有大空白 Space

Xcode 6 iOS 8 Rotation Issue - Large Blank Space On Screen

我 运行 在 Xcode 6 中使用我的 iPad 应用程序遇到了一个非常奇怪的问题。以前(使用 Xcode 5 / iOS 构建时7 SDK)它可以毫无问题地处理旋转,但现在开发人员需要使用 Xcode 6 / iOS 8 SDK 进行构建,我的应用程序不再正确处理旋转。

我做了一些研究并且能够使用 viewWillTransitionToSize 方法来让我的所有子视图正确旋转和调整自己的大小。但是,当我旋转时,我的屏幕旁边有一个大的白色矩形。如果我以纵向启动应用程序并旋转到横向,它会从看起来正常变为右侧有一个大的白色 space。 (如果我从横向开始并旋转到纵向,也会发生同样的事情,但是白色 space 在底部)。

我的子视图确实在适当地调整自己的大小,但不幸的是白色 space 掩盖了它们。我检查了我的 UIScreen window 的边界和框架,这些值似乎有效(横向 1024w x 768h,纵向 768w x 1024h)。以前它是黑色的 space,但是一旦我将行 [self.window setFrame:[[UIScreen mainScreen] bounds]]; 添加到我的 didFinishLaunchingWithOptions 方法中,它就变成了白色的 space。 以下是我正在使用的 viewWillTransitionToSize 代码:

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size WithTransitionCoordinator:coordinator];
    UIInterfaceOrientation *orientation = [self interfaceFromTransform:[coordinator targetTransform]];
    UIInterfaceOrientation *oldOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    [self willRotateToInterfaceOrientation:orientation duration:1.0];

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> content)
    {
        [self willAnimateRotationToInterfaceOrientation:orientation duration:1.0];
    }
    completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
    {
        [self didRotateFromInterfaceOrientation:oldOrientation];
    }];

    CGFloat height = self.view.frame.size.height;
    CGFloat width = self.view.frame.size.width;
    [self.view setFrame:CGRectMake(0, 0, height, width)];
}

interfaceFromTransform 是我创建的一种方法,用于确定转换旋转到哪个界面:

- (UIInterfaceOrientation) interfaceFromTransform: (CGAffineTransform)transform
{
    UIInterfaceOrientation old = [[UIApplication sharedApplication] statusBarOrientation];

    int rotation = 0;

    if (transform.b == -1 && transform.c == 1)
        rotation = 90;
    if (transform.b == 1 && transform.c == -1)
        rotation = -90;
    if (transform.a == -1 && transform.d == -1)
        rotation = 180;

    if (old == UIInterfaceOrientationLandscapeRight)
    {
        if (rotation == 90)
            return UIInterfaceOrientationPortrait;
        if (rotation == -90)
            return UIInterfaceOrientationPortraitUpsideDown;
        if (rotation == 180)
            return UIInterfaceOrientationLandscapeLeft;
    }
    if (old == UIInterfaceOrientationLandscapeLeft)
    {
        if (rotation == 90)
            return UIInterfaceOrientationPortraitUpsideDown;
        if (rotation == -90)
            return UIInterfaceOrientationPortrait;
        if (rotation == 180)
            return UIInterfaceOrientationLandscapeRight;
    }
    if (old == UIInterfaceOrientationPortraitUpsideDown)
    {
        if (rotation == 90)
            return UIInterfaceOrientationLandscapeRight;
        if (rotation == -90)
            return UIInterfaceOrientationLandscapeLeft;
        if (rotation == 180)
            return UIInterfaceOrientationPortrait;
    }
    if (old == UIInterfaceOrientationPortrait)
    {
        if (rotation == 90)
            return UIInterfaceOrientationLandscapeLeft;
        if (rotation == -90)
            return UIInterfaceOrientationLandscapeRight;
        if (rotation == 180)
            return UIInterfaceOrientationPortraitUpsideDown;
    }

    return old;
}

然而尽管如此,那白色space仍然不肯离去。是否有明显遗漏的东西会阻止它在我旋转屏幕时出现?

事实证明,我在我的主要 window nib 文件中禁用了 "Autoresize Subviews"。启用此功能可立即解决问题。