viewWillTransitionToSize:withTransitionCoordinator在模拟器中返回错误的大小

时间:2014-09-29 09:01:01

标签: ios ios-simulator xcode6 size-classes

我喜欢使用此代码在iPhone 6上支持splitViewController:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
    self.forcedTraitCollection = nil;
    if (size.height == 320.0 || size.width == 320.0)
    {
        self.forcedTraitCollection = nil;
    }
    else
    {
        self.forcedTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
    }

    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
}

问题是返回的大小是错误的 - 它为所有模拟器返回CGSize(320,568)。 + n它返回宽度作为高度,反之亦然。

由于

1 个答案:

答案 0 :(得分:2)

我为不同的设备模拟器获取了正确的尺寸,但是在横向模式下,x和y似乎被翻转。我用这个黑客来纠正它:

let mainScreen = UIScreen.mainScreen()
let screenSize = mainScreen.applicationFrame // CGRect screen bounds
var width = size.width
if screenSize.width == size.width {
    // size must be wrong or flipped
    width = size.height
}