1. 程式人生 > >iOS之UITraitCollection

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