将UICollectionViewCell变成普通的UIView可以通过以下步骤实现:

  1. 创建一个新的UIView子类,例如MyView。在这个类中,添加你希望的视图和布局。
class MyView: UIView {
    // 添加视图和布局
}
  1. 在你的UICollectionViewCell子类中,将UICollectionViewCell的contentView替换为MyView的实例。
class CustomCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        let myView = MyView(frame: contentView.bounds)
        myView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        contentView.addSubview(myView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

这样,你的UICollectionViewCell就变成了一个普通的UIView。你可以在MyView中添加你想要的视图和布局,并在CustomCell中使用它。

Swift 如何将UICollectionViewCell变成一个普通的UIView

原文地址: https://www.cveoy.top/t/topic/i5Zx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录