
昨天預(yù)購的手機(jī)到貨了,興奮不已。興奮過后,下載了我之前的用的各種APP ,結(jié)果。。。。不說了,直接上圖!

發(fā)現(xiàn)并沒有適配到上邊的劉海,下邊的操控區(qū)域也是一片黑。這是什么原因?qū)е碌??其?shí)就是網(wǎng)絡(luò)上已經(jīng)傳了很久的安全區(qū)域的問題。
什么是安全區(qū)域
引用一段蘋果官方的話
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 應(yīng)該去適應(yīng)UIKit所定義的安全區(qū)域和布局間隔。安全區(qū)域可以保證所有控件顯示在一個(gè)正確的位置。下圖顯示了安全區(qū)域的位置

iOS 11中,蘋果推出SafeAreaLayoutGuide 取代了 bottomLayoutGuide 和 topLayoutGuide,對于已經(jīng)使用了 bottomLayoutGuide 和 topLayoutGuide 的布局來說,單純使用 safeAreaLayoutGuide 可以完成一樣的工作,同時(shí)也可以完美適配 iPhone X 的布局。使用 Auto Layout 布局的話,safe area 就相對于一個(gè)處于每個(gè) view controller 的視圖層級底層的容器,我們的子控件只需要和它建立約束關(guān)系,就可以完美適配 iPhone X 安全區(qū)域


安全區(qū)域編程
如果使用代碼進(jìn)行布局,對于 safe area 適配也不會復(fù)雜。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
另一個(gè)新的 API additionalSafeAreaInsets 則提供給開發(fā)人員能夠自主擴(kuò)展 safe area 區(qū)域的能力。顧名思義,如果對這個(gè)屬性主動賦值,那么整個(gè)視圖的 safe area 區(qū)域便會發(fā)生變化。
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
調(diào)用時(shí)機(jī)
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.
官方的意思是如果一個(gè)視圖沒有加載到視圖層級或者屏幕可視化,屬性會返回0。換句話說,這個(gè)屬性只有在視圖初始化以后才能賦值。
具體的,這個(gè)屬性只有在viewDidLayoutSubviews和viewDidAppear才會生成,而在loadView? viewDidLoad? viewWillAppear是不會生成的。
Home Indicator
Home indicator 的設(shè)置類似于 prefersStatusBarStyle,iOS 11 增加了 UIViewController 的一個(gè) UIHomeIndicatorAutoHidden 分類來控制 home 鍵的自動隱藏。通常在全屏播放視頻,全屏游戲等場景下會需要用到此特性。
常見高度
