前言:iOS7及其以上系統(tǒng),默認的將視圖的擴充區(qū)域延伸至系統(tǒng)通知欄頂部,也就是說默認情況下的視圖控制器視圖區(qū)域是全屏的,那么如果view上存在子視圖并且該子視圖為scrollView類型的時候,對于scrollView的布局蘋果有做了些特殊處理。
示例說明:當(dāng)我們把scrollView的frame默認設(shè)置為view的bounds的時候發(fā)現(xiàn),位于scrollView上的子視圖基于scrollView為參考系放置,如果位于(0,0)坐標下并沒有被導(dǎo)航欄遮蓋,這是為什么呢,原因就是scrollView的兩個屬性值發(fā)生了變化,contentInset.top和contentOffset.y,其中contentInset.top變?yōu)榱?4,contentOffset.y變更為了-64,這樣我們的scrollView上的視圖就正好顯示到了導(dǎo)航欄的下方而不會被遮擋。
示例相關(guān)點解釋 :首先我們了解一下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內(nèi)嵌距離,后者是content View origin相對于scrollView origin的偏移距離。所以當(dāng)內(nèi)嵌距離為64時,content view 的origin 相對于scrollView origin的偏移量為y:-64,所以contentOffset 為-64;
所以,當(dāng)content view的頂部內(nèi)嵌距離scrollView為64時,相當(dāng)于此時scrollView會展示多余的頂部64個像素的內(nèi)容,這和contentOffset為-64,的下拉效果是一樣的。
注意:
對于一些需要改變contentInset的scrollView,我們可以將contentInset的設(shè)置放置在viewController的viewDidLayoutSubViews的方法中,這樣可以避免一些異常發(fā)生。