前言:iOS7及其以上系統,默認的將視圖的擴充區域延伸至系統通知欄頂部,也就是說默認情況下的視圖控制器視圖區域是全屏的,那么如果view上存在子視圖并且該子視圖為scrollView類型的時候,對于scrollView的布局蘋果有做了些特殊處理。
示例說明:當我們把scrollView的frame默認設置為view的bounds的時候發現,位于scrollView上的子視圖基于scrollView為參考系放置,如果位于(0,0)坐標下并沒有被導航欄遮蓋,這是為什么呢,原因就是scrollView的兩個屬性值發生了變化,contentInset.top和contentOffset.y,其中contentInset.top變為了64,contentOffset.y變更為了-64,這樣我們的scrollView上的視圖就正好顯示到了導航欄的下方而不會被遮擋。
示例相關點解釋 :首先我們了解一下contentInset和contentOffset這兩個概念:
contentInset:The distance that the content view is inset from the enclosing scroll view.
contentOffset:The point at which the origin of the content view is offset from the origin of the scroll view.
contentSize :The size of the content view
即:前者為content View相對于scrollView內嵌距離,后者是content View origin相對于scrollView origin的偏移距離。所以當內嵌距離為64時,content view 的origin 相對于scrollView origin的偏移量為y:-64,所以contentOffset 為-64;
所以,當content view的頂部內嵌距離scrollView為64時,相當于此時scrollView會展示多余的頂部64個像素的內容,這和contentOffset為-64,的下拉效果是一樣的。
注意:
對于一些需要改變contentInset的scrollView,我們可以將contentInset的設置放置在viewController的viewDidLayoutSubViews的方法中,這樣可以避免一些異常發生。