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

iOS之UITraitCollection相关推荐

  1. iOS开发之AVKit框架使用

    2019独角兽企业重金招聘Python工程师标准>>> iOS开发之AVKit框架使用 一.引言 在iOS开发框架中,AVKit是一个非常上层,偏应用的框架,它是基于AVFounda ...

  2. ios单应用模式_如何为iOS 13暗模式设置应用

    ios单应用模式 Apple launched the much-awaited iOS 13 updates globally on September 19 across all iPhones ...

  3. iOS 使用UI控件的外观协议UIAppearance进行设置默认UI控件样式

    在iOS开发中,经常会对UINavigationBar的样式进行全局样式.采用的设置方式有两种: 第一种,采用方式如下: [UINavigationBar appearance] 这种是对一类对象的默 ...

  4. iOS开发系列--音频播放、录音、视频播放、拍照、视频录制(转)

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

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

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

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

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

  7. iOS开发 DarkMode 暗黑模式

    一.开发环境 Xcode11 正式版 iOS13 beta版本 Mac系统: 10.15 beta版本 二.适配原理 将同一个资源,创建出两种模式的样式.系统根据当前选择的样式,自动获取该样式的资源. ...

  8. iOS开发 黑暗模式

    iOS 13终于引来了暗黑模式. 每当新特性的到来,iOS开发者们既紧张又有点小兴奋,怀揣着被虐的心态,让我们来看看暗黑模式到底是个啥. 虽然这是一个设计驱动的需求,但是本着一颗虔诚(暴躁)的心,我们 ...

  9. ios适配暗黑模式-图片、颜色

    一.适配Dark mode:颜色适配.图片适配 其实适配Dark模式,开发者主要从颜色和图片两个方面进行适配,我们不需要关心切换模式时改如何操作,这些都是系统帮我们实现,我们只需要做好适应两套模式的资 ...

最新文章

  1. 清华镜像源地址_PyCharm安装第三方库(内含添加国内镜像源方法)
  2. matlab基本运算与函数
  3. 面试总结之MISC(操作系统,网络,数学,软件开发,测试,工具,系统设计,算法)...
  4. php网站开发期末大作业,大学生期末网页大作业
  5. Linux 下编译并安装配置 Qt
  6. oracle exec编译失效,编译oracle失效的函数、存储过程、视图等
  7. 云服务器怎么管理,第一次使用云服务器,如何使用和管理云服务器
  8. 第十七节:ES6新增的Map和WeakMap 又是什么东西?
  9. gem install sass 本地配置和淘宝源无效的解决办法
  10. 无法创建 ActiveX 组件(转载)
  11. Unity MRTK RadialView
  12. FreeDOS 24 周年,创始人分享常用 DOS 命令备忘表
  13. 视频教程-三天掌握三菱FX系列PLC视频教程-单片机/工控
  14. RestSharp使用说明
  15. Android 补间动画TranslateAnimation 位移效果
  16. 如何0成本搭建外卖CPS返利小程序
  17. 手机UC浏览器导出书签到电脑Chrome浏览器中
  18. Android开发最佳实践
  19. List集合 查询重复数据,查询重复次数
  20. 大数据 -- 数据倾斜

热门文章

  1. 【数据结构基础笔记】【队列】
  2. Java文件类boolean setExecutable(boolean exec_file,boolean owner_access)方法,带示例
  3. python 子图大小_Python | 图的大小
  4. mysql创建表时出现1071_mysql 出现1071错误怎么办
  5. python安全攻防---爬虫基础---BeautifulSoup解析
  6. php 查看 实例 的方法,php – 从Laravel 5.1中的通用数据库查询中获取Eloquent模型的实例...
  7. 2016年湖南省第十二届大学生计算机程序设计竞赛
  8. C++ const 与 extern
  9. 575 div3RGB Substring (hard version)——思维-
  10. System V 信号量