iPhone  X适配问题

iPhone X

昨天预购的手机到货了,兴奋不已。兴奋过后,下载了我之前的用的各种APP ,结果。。。。不说了,直接上图!


猫眼

发现并没有适配到上边的刘海,下边的操控区域也是一片黑。这是什么原因导致的?其实就是网络上已经传了很久的安全区域的问题。

什么是安全区域

引用一段苹果官方的话

All apps should adhere to the safe area and layout margins defined by UIKit, which ensure appropriate insetting based on the device and context,The safe area also prevents content from underlapping the status bar, navigation bar, toolbar, and tab bar

大体意思就是说所有的APP 应该去适应UIKit所定义的安全区域和布局间隔。安全区域可以保证所有控件显示在一个正确的位置。下图显示了安全区域的位置


蓝色区域为安全区域

iOS 11中,苹果推出SafeAreaLayoutGuide 取代了 bottomLayoutGuide 和 topLayoutGuide,对于已经使用了 bottomLayoutGuide 和 topLayoutGuide 的布局来说,单纯使用 safeAreaLayoutGuide 可以完成一样的工作,同时也可以完美适配 iPhone X 的布局。使用 Auto Layout 布局的话,safe area 就相对于一个处于每个 view controller 的视图层级底层的容器,我们的子控件只需要和它建立约束关系,就可以完美适配 iPhone X 安全区域


上下左右依附在safe area,而不是以前的屏幕


效果图

安全区域编程

如果使用代码进行布局,对于 safe area 适配也不会复杂。iOS 11 中 UIView 的新 API SafeAreaInsets 和 UIViewController 的新 API additionalSafeAreaInsets 能够很方便地解决代码布局的适配问题。

var safeAreaInsets: UIEdgeInsets { get } 
You might use this property at runtime to adjust the position of your view's content programmatically

另一个新的 API additionalSafeAreaInsets 则提供给开发人员能够自主扩展 safe area 区域的能力。顾名思义,如果对这个属性主动赋值,那么整个视图的 safe area 区域便会发生变化。

var additionalSafeAreaInsets: UIEdgeInsets { get set }
Use this property to adjust the safe area insets of this view controller's views by the specified amount. The safe area defines the portion of your view controller's visible area that is guaranteed to be unobscured by the system status bar or by an ancestor-provided view such as the navigation bar.
You might use this property to extend the safe area to include custom content in your interface. For example, a drawing app might use this property to avoid displaying content underneath tool palettes

调用时机

If the view is not currently installed in a view hierarchy, or is not yet visible onscreen, the edge insets in this property are 0.

官方的意思是如果一个视图没有加载到视图层级或者屏幕可视化,属性会返回0。换句话说,这个属性只有在视图初始化以后才能赋值。

具体的,这个属性只有在viewDidLayoutSubviews和viewDidAppear才会生成,而在loadView  viewDidLoad  viewWillAppear是不会生成的。

Home Indicator

Home indicator 的设置类似于 prefersStatusBarStyle,iOS 11 增加了 UIViewController 的一个 UIHomeIndicatorAutoHidden 分类来控制 home 键的自动隐藏。通常在全屏播放视频,全屏游戏等场景下会需要用到此特性。

常见高度


常见高度
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容