将 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/mAMJ 著作权归作者所有。请勿转载和采集!

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