iPad的风景和肖像与大小类不同的布局

如何使用Size类devise具有不同布局的iPad横向和纵向屏幕。 我只能在两个方向上findw-正则和h-正则。 例如:我需要使用Size Class纵向alignment2个视图,横向alignment2个视图

最后我find了解决办法:

if traitCollection.verticalSizeClass == .Regular && traitCollection.horizontalSizeClass == .Regular { var orientation:UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation; if orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight { // orientation is landscape } else { // orientation is portrait } } 

这似乎是苹果的意图,对待iPad的方向是一样的 – 但正如我们许多人所发现的,有一个非常合法的devise理由,想改变iPad的肖像与iPad景观的UI布局。

但是,请参阅此答案的另一种方法来resize类来做我们需要的: https : //stackoverflow.com/a/28268200/4517929

对于Swift 3来说,它看起来像这样:

 override func overrideTraitCollection(forChildViewController childViewController: UIViewController) -> UITraitCollection? { if UI_USER_INTERFACE_IDIOM() == .pad && view.bounds.width > view.bounds.height { let collections = [UITraitCollection(horizontalSizeClass: .regular), UITraitCollection(verticalSizeClass: .compact)] return UITraitCollection(traitsFrom: collections) } return super.overrideTraitCollection(forChildViewController: childViewController) } 

它将在横向模式下使用wRhC而不是wRhR用于iPad设备。 把这段代码放到你的基本视图控制器中,也就是说,这个规则将适用于这个控制器。 您可以在此处添加任何附加条件…例如,如果您希望此规则仅适用于特定的视图控制器,那么您的if运算符将如下所示:

  if UI_USER_INTERFACE_IDIOM() == .pad && childViewController is YourSpecificViewController && view.bounds.width > view.bounds.height { let collections = [UITraitCollection(horizontalSizeClass: .regular), UITraitCollection(verticalSizeClass: .compact)] return UITraitCollection(traitsFrom: collections) }