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

iOS之UITraitCollection的更多相关文章

  1. iOS开发系列--音频播放、录音、视频播放、拍照、视频录制

    --iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...

  2. IOS多媒体

    概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像 ...

  3. 《转》iOS音频视频初级开发

    代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS   ...

  4. iOS开发----音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  5. iOS音频

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  6. iOS界面开发

    [转载] iOS界面开发 发布于:2014-07-29 11:49阅读数:13399 iOS 8 和 OS X 10.10 中一个被强调了多次的主题就是大一统,Apple 希望通过 Hand-off ...

  7. iOS开发~UI布局(三)深入理解autolayout

    一.概要 通过对iOS8界面布局的学习和总结,发现autolayout才是主角,autolayout是iOS6引入的新特性,当时还粗浅的学习了下,可是没有真正应用到项目中.随着iOS设备尺寸逐渐碎片化 ...

  8. iOS:Size Classes的使用

    iOS 8在应用界面的可视化设计上添加了一个新的特性-Size Classes,对于任何设备来说,界面的宽度和高度都只分为两种描述:正常和紧凑.这样开发者便可以无视设备具体的尺寸,而是对这两类和它们的 ...

  9. iOS 9之分屏多任务(Split View)

    金田(github 示例源码) 多任务(multitasking)算是iOS9中最引人瞩目的核心新特性了,之前越狱版用户就用过类似的插件,微软的 苏菲 (Windows Surface)系列也有分屏多 ...

随机推荐

  1. for in 和 for of的区别详解

    for in 和 for of 相对于大家肯定都不陌生,都是用来遍历属性的没错.那么先看下面的一个例子: 例1 const obj = { a: 1, b: 2, c: 3 } for (let i ...

  2. JSON.parse(text[, reviver])

    1. JSON.parse(text[, reviver])text 必需 有效的json字符串reviver 可选 函数 2. 举栗子1) 只有第一个参数 let objStr = '{" ...

  3. python 时间加8小时后的时间

    eta_temp = one['arrival'].encode('utf-8') fd = datetime.datetime.strptime(eta_temp, "%Y-%m-%dT% ...

  4. gitlab文件夹的权限不要随便给777

    gitlab  权限给到777   不一定有用

  5. 嵌入式开发 centos7 交叉编译环境准备

    1. 安装centos7,启动图像化界面. 参考:https://blog.csdn.net/qq_23014435/article/details/74347925 # systemctl get- ...

  6. 一次完整的HTTP请求需要的7个步骤

    HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1:建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...

  7. 精读《Epitath 源码 - renderProps 新用法》

    1 引言 很高兴这一期的话题是由 epitath 的作者 grsabreu 提供的. 前端发展了 20 多年,随着发展中国家越来越多的互联网从业者涌入,现在前端知识玲琅满足,概念.库也越来越多.虽然内 ...

  8. 【函数应用】PHP中关于URL的函数处理

    一,函数介绍 1.解析HTTP头信息:get_header() array get_headers ( string 目标URL [, int $format = 0 [如果将可选的 format 参 ...

  9. 怎样查看web软件例如apache的连接数

    查看连接总数和当前的连接数 netstat -ant | grep $ip:80 | wc -l netstat -ant | grep $ip:80 | grep EST | wc -l 查看IP访 ...

  10. HDU - 6514 Monitor(二维差分)

    题意 给定一个\(n×m\)的矩阵.(\(n×m <= 1e7\)). \(p\)次操作,每次可以在这个矩阵中覆盖一个矩形. \(q\)次询问,每次问一个矩形区域中,是否所有的点都被覆盖. 解析 ...