本文的Demo工程代碼參考這里的StudyUIViewAndUIVC
目錄
布局
layoutSubviews
- 原型
// UIView
- (void)layoutSubviews;
- 作用
Subclasses can override this method as needed to perform more precise layout of their subviews, 即對(duì)subviews重新布局
- 觸發(fā)
1: 初始化時(shí)在viewDidLoad(UIViewController)之后觸發(fā)
2: frame change, addSubview以及UIScrollView滾動(dòng)時(shí)觸發(fā)
sub view frame change以及旋轉(zhuǎn)屏幕時(shí)會(huì)觸發(fā)其父view的layoutSubviews
- 備注
1: 該方法不能直接調(diào)用, 必須通過(guò)setNeedsLayout或layoutIfNeeded讓系統(tǒng)自動(dòng)觸發(fā)該方法
2: You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want
setNeedsLayout與layoutIfNeeded有什么區(qū)別? setNeedsLayout只是標(biāo)記需要重新布局, 但不立即刷新, 配合layoutIfNeeded可以立即更新
重繪
drawRect
- 原型
// UIView
- (void)drawRect:(CGRect)rect;
- 作用
Draws the receiver’s image within the passed-in rectangle, 即重繪
- 觸發(fā)
1: 初始化時(shí)在viewDidLoad(UIViewController)之后觸發(fā)
2: 調(diào)用setNeedsDisplay或setNeedsDisplayInRect時(shí)觸發(fā)
- 備注
1: 該方法不能直接調(diào)用, 必須通過(guò)setNeedsDisplay或setNeedsDisplayInRect讓系統(tǒng)自動(dòng)觸發(fā)該方法
2: 觸發(fā)drawRect有一個(gè)必要的前提: rect不能為0
參考
談?wù)刄IView的幾個(gè)layout方法-layoutSubviews攒射、layoutIfNeeded棠涮、setNeedsLayout...
What is the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews?
更多文章, 請(qǐng)支持我的個(gè)人博客