1. 程式人生 > >contentSize、contentInset、contentOffset的區別

contentSize、contentInset、contentOffset的區別


在專案中經常會使用到contentSize、contentInset、contentOffset 這三個屬性,時間一長很容易被搞混,打算記一下。

我們首先了解下什麼叫frame 與 bounds 、center 屬性。

我們來先看一張圖




frame 是 x、y、width、height 相當於 左邊圖:(40、40、240、380)

bounds 是 width、height  x與y 預設都是0  相當於 右邊圖 (0、0、240、380)

center 是一個View的中心,預設是這個view對應的superview 座標的中間值 

相當於 左邊圖:(160、230)




說完了上面的 該說正題了。


contentSize:

contentSize是scrollview可以滾動的區域,比如frame = (0 ,0 ,320 ,480) contentSize = (320 ,960),代表你的scrollview可以上下滾動,滾動區域為frame大小的兩倍。


contentInset:

contentInset的單位是UIEdgeInsets,預設值為UIEdgeInsetsZero,也就是沒有擴充套件的邊。 UIEdgeInsets 設定包圍tableView的座標,typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right;  // specify amount to inset (positive) for each of the edges. values can be negative to 'outset' } UIEdgeInsets; 代表 上、 左、 下、 右 四個方向擴展出去的值。

contentOffset: contentOffset是UIScrollView當前顯示區域的頂點相對於frame頂點的偏移量,例如上面的例子如果拉到最下面,則contentOffset就是(0, 480),也就是y偏移了480.