我目前正在研究如何以编程方式使用大小类,这里有一些对您有用的发现:
找出最简单的方法是检查视图控制器 traitCollection
po self.traitCollection
或收听过渡:
首先使用 UIContentContainer 协议
@interface ViewController : UIViewController<UIContentContainer>
然后实现willTransitionToTraitCollection:
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
}
newCollection 元素包含可用于您的目的的信息,这是将 iPhone 6 旋转到横向时返回的信息:
<UITraitCollection: 0x7f9ad152f320; _UITraitNameUserInterfaceIdiom = Phone, _UITraitNameDisplayScale = 2.000000, _UITraitNameHorizontalSizeClass = Compact, _UITraitNameVerticalSizeClass = Compact, _UITraitNameTouchLevel = 0, _UITraitNameInteractionModel = 1>
和肖像:
<UITraitCollection: 0x7f9ad142d770; _UITraitNameUserInterfaceIdiom = Phone, _UITraitNameDisplayScale = 2.000000, _UITraitNameHorizontalSizeClass = Compact, _UITraitNameVerticalSizeClass = Regular, _UITraitNameTouchLevel = 0, _UITraitNameInteractionModel = 1>
从那里你可以看到它对横向和垂直使用紧凑尺寸类,但在纵向使用常规尺寸类。