- addSubView
- 更改父视图的大小
- UIScrollView滚动的时候
- 旋转屏幕
为什么需要layoutSubviews?
有时我们需要在layoutSubviews获取当前view的正确大小
情形1:
在UITableViewCell上添加一个UICollectionView,要求此UICollectionView的大小和UITableViewCell的大小一致,需要在layoutSubviews中设置UICollectionView的大小才是正确的。
代码
override func layoutSubviews() {
super.layoutSubviews()
collectionView.frame = CGRect(x: 0, y: 0, width: self.width, height: self.height)
}
网友评论