iOS開發之UIScrollView中frame、contentSize、contentOffset、contentInset
阿新 • • 發佈:2019-01-25
UIScrollView有三個屬性,分別為contentSize、contentOffset、contentInset,再加上UI View的屬性frame,共四個屬性,由於這四個屬性存在一定的關聯,不仔細很容易弄混,所以抽時間好好了解了一下。
UIScrollView的四個屬性在官方文件中是這樣的:
/*
use bounds/center and not frame if non-identity transform.
if bounds dimension is odd, center may be have fractional part
*/
@property(nonatomic ) CGRect frame;
// default CGSizeZero
@property(nonatomic) CGSize contentSize;
// default CGPointZero
@property(nonatomic) CGPoint contentOffset;
// default UIEdgeInsetsZero. add additional scroll area around content
@property(nonatomic) UIEdgeInsets contentInset;
frame
frame是scrollView檢視的位置和大小,為一個矩形框,資料型別為CGRect。
frame資料型別為CGRect,有四個引數值,分別為
- frame.origin.x: 表示檢視位置原點的x值
- frame.origin.y: 表示檢視位置原點的y值
- frame.size.width: 表示檢視的寬度
- frame.size.height: 表示檢視的高度
contentSize
contentSize是scrollView需要顯示的內容大小,是一個矩形框,因為contentSize是可以滾動的,因此只有大小而沒有位置,資料型別為CGSize。
contentSize資料型別為CGSize,只有兩個引數,分別為
- contentSize.width: 表示顯示內容的寬度
- contentSize.height: 表示顯示內容的高度
contentOffset
contentOffset是scrollView的contentSize的原點相對於frame原點的偏移量,是兩個點之間的相對值,資料型別為CGPoint。
contentOffset資料型別為CGPoint,只有兩個引數,分別為
- contentOffset.x: 表示contentSize在x方向上的偏移量(有正負值)
- contentOffset.y: 表示contentSize在y方向上的偏移量(有正負值)
contentInset
contentInset是scrollView的contentSize相對於自身四個邊框的顯示範圍變化值,資料型別為UIEdgeInsets。
contentInset資料型別為UIEdgeInsets,有四個引數,分別為
- contentInset.top: 表示contentSize上邊距顯示範圍變化值(有正負值)
- contentInset.bottom: 表示contentSize下邊距顯示範圍變化值(有正負值)
- contentInset.left: 表示contentSize左邊距顯示範圍變化值(有正負值)
- contentInset.right: 表示contentSize右邊距顯示範圍變化值(有正負值)
frame、contentSize、contentOffset、contentInset四者之間的關係
frame、contentSize、contentOffset、contentInset四者之間存在著一定的關係,見下圖。