View Controller学习笔记—第四章Adaptivity and Size Change

各位iOS开发大佬们好:
我是一名Swift+SwiftUI栈的iOS小白,目前还在上大三,最近准备实习,面试的过程中发现现在大公司很多还在用OC + UIKit的技术栈,OC我还在考虑要不要学,目前想先把UIKit学完,这是我在官网学习UIKit英文文档时摘录的本人认为的重点,如果你们也觉得对你们有用的话欢迎持续关注,我大概一天更一节,有事除外。格式什么的我也就不做了,翻译都是我自己翻译的,哪里不对欢迎在评论区指正,感谢各位大佬支持

今天2021年12月14日
这一章叫做Adaptivity and Size Change

The Adaptive Model

当约束不足以管理布局时可以用traits,VC,View和其他一部分对象管理了traits的集合,指定了关联对象的环境
以下是部分traits
在这里插入图片描述

用traits决定如何展现视图。许多UIKit类根据你指定的traits定制信息。
下面是一些使用各种类型traits的小提示:
Use size classes to make coarse changes to your interface. Size class changes are an appropriate time to add or remove views, add or remove child view controllers, or change your layout constraints. You can also do nothing and let your interface adapt automatically using its existing layout constraints.
使用size类做粗略改变,比如添加或移除View或子VC,或改变布局约束
Never assume that a size class corresponds to the specific width or height of a view. Your view controllers’ size classes can change for many reasons. For example, a container view controller on iPhone might make one of its children horizontally regular to force it to display its contents differently.
不要以为size类会和view的特定尺寸一致,size类会因很多因素而发生改变。
Avoid using idiom information to make decisions about the layout or content of your interface. Apps running on iPad and iPhone should generally display the same information and should use size classes to make layout decisions.
不要用不靠谱的消息改变界面的布局或内容
Trait不经常发生,UIKit会根据底层环境改变更新VC的traits
Size class traits are much more likely to change than the display scale trait. The idiom trait should rarely, if ever, change. Size class changes occur for the following reasons:
The vertical or horizontal size class of the view controller’s window changed, usually because of a device rotation.
The horizontal or vertical size class of a container view controller changed.
The horizontal or vertical size class of the current view controller was changed explicitly by its container.
size traits发生无非以下几种情况:
VC的window横竖尺寸改变,一般是设备旋转
容器VC的横竖尺寸改变
VC因为容器VC横竖尺寸改变而显式改变

Size类改变会影响到该视图在视图层级中的所有子视图
The window size change is propagated down the view controller hierarchy along with any corresponding trait changes. For each view controller in the hierarchy, UIKit calls the following methods to report those changes:
The willTransitionToTraitCollection:withTransitionCoordinator: tells each relevant view controller that its traits are about to change.
The viewWillTransitionToSize:withTransitionCoordinator: tells each relevant view controller that its size is about to change.
The traitCollectionDidChange: tells each relevant view controller that its traits have now changed.
When walking the view controller hierarchy, UIKit reports changes to a view controller only when there is a change to report. If a container view controller overrides the size classes of its children, those children are not notified when the container’s size class changes. Similarly, if a view controller’s view has a fixed width and height, it does not receive size change notifications.
Window尺寸改变会影响到当前VC层级中的所有VC,引发相同的traits改变,层级中的每个VC都会被UIKit以以下方式通知:
willTransitionToTraitCollection:withTransitionCoordinator: 告诉所有有关VC的traits将要改变
viewWillTransitionToSize:withTransitionCoordinator:告诉所有有关VC的尺寸将要改变
traitCollectionDidChange: 通知所有有关VC的traits已经改变完成
UIKit只会通知应该通知的,如果一个容器VC重写了子VC的size类,那么当容器VC的size类发生改变时子VC将不会被通知,如果子VC的长宽是混合的,那么它也不会被通知。
在这里插入图片描述

Never assume that your app will be displayed with a specific size class on a device. Always check the size class found in an object’s trait collection when making decisions about how to configure that object.
不要觉得app会自己显示成特定的size,当决定如何配置对象时,时刻检查它traits的size类

Building an Adaptive Interface

一个适应性强的界面应该能响应trait和size的改变,在VC级别使用traits做一些粗略改变,大的调好了就该微调了。
子VC默认继承父VC的traits,对于一些traits,父VC的不一定在子VC上也管用
When implementing a container view controller, you modify the traits of the child by calling the setOverrideTraitCollection:forChildViewController: method of the container view controller. You create a new set of traits and associate them with a child view controller. You execute this code from your parent view controller and only need to do so once. Overridden traits remain with the child until you change them again or until you remove the child from your view controller hierarchy
When the traits of the parent view controller change, children inherit any traits that are not explicitly overridden by the parent. For example, when the parent’s horizontal size class changes from regular to compact, the child in the preceding example retains its regular horizontal size class. However, if the displayScale trait changes, the child inherits the new value.
当初始化容器VC时,通过调用容器VC的setOverrideTraitCollection:forChildViewController: 方法修改子VC的traits,创建一个新traits集合并与子VC关联,从父VC那执行代码并且仅执行一次,被重写的traits保留子VC直到你再一次修改它们或者你不再需要他们
当父VC的traits改变时,子VC继承所有没有被父VC显式重写的traits,比如size就不会跟着变,但如果是displayScale就会跟着变
For some apps, adapting to a full-screen style may present problems. For example, a popover is normally dismissed by tapping outside its bounds, but doing so is not possible in a compact environment where the popover covers the entire screen, as shown in Figure 13-3. When the default adaptation style is not appropriate, you can tell UIKit to use a different style or present an entirely different view controller that is better suited to the full-screen style.
Figure 13-3A popover in regular and compact environments
在这里插入图片描述

presented VC自动适配横竖屏,过渡的时候默认将 presentation style变为UIModalPresentationFullScreen,对于自定义的风格 presentation控制器可以根据它决定适配情况。但对于某些视图来说,全屏就不合适,比如全屏的弹出框,因为弹出框在点击区域外才会消失,当适配模式不合适时,可以更换或显示其他适合全屏的VC

To change the default adaptive behavior for a presentation style, assign a delegate to the associated presentation controller. You access the presentation controller using the presented view controller’s presentationController property. The presentation controller consults your delegate object before making any adaptivity-related changes. The delegate can return a different presentation style than the default and it can provide the presentation controller with an alternate view controller to display.
Use the delegate’s adaptivePresentationStyleForPresentationController: method to specify a different presentation style than the default. When transitioning to a compact environment, the only supported styles are the two full-screen styles or UIModalPresentationNone. Returning UIModalPresentationNone tells the presentation controller to ignore the compact environment and continue using the previous presentation style. In the case of a popover, ignoring the change gives you the same iPad-like popover behavior on all devices. Figure 13-4 shows the default full-screen adaption and no adaptation side by side so that you can compare the resulting presentations.
Figure 13-4Changing the adaptive behavior for a presented view controller
在这里插入图片描述

To replace the view controller altogether, implement the delegate’s presentationController:viewControllerForAdaptivePresentationStyle: method. When adapting to a compact environment, you might use that method to insert a navigation controller into your view hierarchy or load a view controller that was specifically designed for the smaller space.

通过赋一个代理给有关的presentation控制器来更改适配模式,通过访问新VC的presentationController 属性来访问它的presentation控制器。presentation控制器在做和适配有关的更改时需要询问代理对象,代理可以返回一个不同的presentation风格,用adaptivePresentationStyleForPresentationController: 方法指定其他的显示风格,当过渡到紧凑情况下时,支持的风格只有两种全屏风格或UIModalPresentationNone.,UIModalPresentationNone.意味着他会忽略紧凑并沿用之前的风格
实现代理的presentationController:viewControllerForAdaptivePresentationStyle: 方法一起替换VC,当适配紧凑环境时,你可能会用它插入navigation控制器到层级中或为紧凑环境特制的VC

实现适配弹出框的小贴士
弹出框在从横全屏到横分屏之间需要更多的操作改变,当变成紧凑环境时默认弹出框全屏,影响到了弹出框最好的消失方式(点击弹出框外侧)。 通过以下方式补偿
Push the popover’s view controller onto an existing navigation stack. When there is a parent navigation controller available, dismiss the popover and push its view controller onto the navigation stack.
Add controls to dismiss the popover when it is presented full-screen. You can add controls to the popover’s view controller, but a better option is to swap out the popover for a navigation controller using the presentationController:viewControllerForAdaptivePresentationStyle: method. Using a navigation controller gives you a modal interface and space to add a Done button or other controls to dismiss the content.
Use a presentation controller delegate to eliminate any adaptivity changes. Get the popover presentation controller and assign a delegate to it that implements the adaptivePresentationStyleForPresentationController: method. Returning UIModalPresentationNone from that method causes the popover to continue to be displayed as a popover.

尺寸可能由于许多原因改变:比如:
The dimensions of the underlying window change, usually because of an orientation change. 底层window尺寸改变(坐标改变)
A parent view controller resizes one of its children. 父VC重新计算子VC的大小
A presentation controller changes the size of its presented view controller.旧VC改变新VC的大小
When size changes happen, UIKit automatically updates the size and position of the visible view controller hierarchies through the normal layout process. If you specified the size and position of your views using Auto Layout constraints, your app adapts automatically to any size changes and should run on devices with different screen sizes.
If your Auto Layout constraints are insufficient to achieve the look you want, you can use the viewWillTransitionToSize:withTransitionCoordinator: method to make changes to your layout. You can also use that method to create additional animations to run alongside the size-change animations. For example, during an interface rotation, you might use the transition coordinator’s targetTransform property to to create a counter-rotation matrix for parts of your interface.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值