iOS之UITraitCollection

UITraitCollection

为表征 size class 而生,用来区分设备。你可以在它身上获取到足以区分所有设备的特征。

UITraitEnvironment 协议、UIContentContainer 协议

UIViewController 遵循了这两个协议,用来监听和设置 traitCollection 的变化。

@protocol UITraitEnvironment <NSObject>

@property (nonatomic, readonly) UITraitCollection *traitCollection NS_AVAILABLE_IOS(8_0);

/*! To be overridden as needed to provide custom behavior when the environment's traits change. */
- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection NS_AVAILABLE_IOS(8_0);
@end

UIViewController 对 UIContentContainer 协议提供了默认的实现。我们自定义 ViewController 的时候可以重写这些方法来调整视图布局,比如我们可以在这些方法里调整 ChildViewControler 的位置。当我们重写这些协议方法时,我们通常都去调用 super。

@protocol UIContentContainer <NSObject>

preferredContentSize 在 UIContentContainer 协议中是只读的,对应的 UIViewController 有可写的版本。我们可以使用 preferredContentSize 来设置我们期望的 ChildViewController 的界面大小。举个例子,如果应用中使用的 popOver 大小会发生变化,iOS7 之前我们可以用 contentSizeForViewInPopover 来调整。iOS7 开始这个 API 被废弃,我们可以使用 preferredContentSize 来设置。

当一个容器 ViewController 的 ChildViewController 的这个值改变时,UIKit 会调用 preferredContentSizeDidChangeForChildContentContainer 这个方法告诉当前容器 ViewController 。我们可以在这个方法里根据新的 Size 对界面进行调整。

@property (nonatomic, readonly) CGSize preferredContentSize NS_AVAILABLE_IOS(8_0);

- (void)preferredContentSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container NS_AVAILABLE_IOS(8_0);

/*
Intended as a bridge for a view controller that does not use auto layout presenting a child that does use auto layout.

If the child's view is using auto layout and the -systemLayoutSizeFittingSize: of the view
changes, -systemLayoutFittingSizeDidChangeForChildContentContainer: will be sent to the view controller's parent.
*/
- (void)systemLayoutFittingSizeDidChangeForChildContentContainer:(id <UIContentContainer>)container NS_AVAILABLE_IOS(8_0);

/*
When the content container forwards viewWillTransitionToSize:withTransitionCoordinator: to its children, it will call this method to determine what size to send them. 

If the returned size is the same as the child container's current size, viewWillTransitionToSize:withTransitionCoordinator: will not be called.

设置 ChildViewController 的 size。当容器ViewControllerviewWillTransitionToSize:withTransitionCoordinator:被调用时(我们重写这个方法时要调用 super),sizeForChildContentContainer 方法将会被调用。然后我们可以把需要设置的 size 发送给 ChildViewController。当我们设置的这个 size 和当前 ChildViewController 的 size 一样,那么 ChildViewController 的 viewWillTransitionToSize 方法将不会被调用。默认的实现是返回 parentSize。
*/
- (CGSize)sizeForChildContentContainer:(id <UIContentContainer>)container withParentContainerSize:(CGSize)parentSize NS_AVAILABLE_IOS(8_0);

/* 
This method is called when the view controller's view's size is changed by its parent (i.e. for the root view controller when its window rotates or is resized). 

If you override this method, you should either call super to propagate the change to children or manually forward the change to children.

ViewController 的 View 的 size 被他的 Parent Controller 改变时,会触发这个方法。(比如rootViewController 在它的 window 旋转的时候)。我们在重写这个方法时,确保要调用 super,来保证 size 改变的这条消息能够正常传递给它的 Views 或者 ChildViewControllers。

*/
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

/* 
This method is called when the view controller's trait collection is changed by its parent.

If you override this method, you should either call super to propagate the change to children or manually forward the change to children.

当 ViewController 的 traitCollection 的值将要改变时会调用这个方法。这个方法是在  UITraitEnvironment 协议方法 traitCollectionDidChange: 之前被调用。我们在重写这个方法时,也要确保要调用 super 来保证消息的传递。
*/
- (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator NS_AVAILABLE_IOS(8_0);

@end 
原文地址:https://www.cnblogs.com/henusyj-1314/p/9928560.html