美文网首页
iOS开发中的layoutSubviews方法的调用时机

iOS开发中的layoutSubviews方法的调用时机

作者: 梁森的简书 | 来源:发表于2021-03-13 10:54 被阅读0次
    1. addSubView
    2. 更改父视图的大小
    3. UIScrollView滚动的时候
    4. 旋转屏幕

    为什么需要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)
        }
    

    相关文章

      网友评论

          本文标题:iOS开发中的layoutSubviews方法的调用时机

          本文链接:https://www.haomeiwen.com/subject/zbkaihtx.html