ios - UICollectionView 的框架未正确更新为 UITableViewCell subview

标签 ios swift uitableview uicollectionview

我将 UICollectionView 放入自定义 UITableView 中。 UICollectionView 框架设置为 contentView 边界,但它并没有完全接受它。

CellForRowAtIndexPath

if indexPath.row == 0 {
    let cell = tableView.dequeueReusableCell(withIdentifier: "collectionViewCell") as! TravelTypeTableViewCell
    cell.backgroundColor = .green
    return cell
} else {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    cell.textLabel?.text = "adsf"
    return cell
}

TableViewCell

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    let layout = UICollectionViewFlowLayout()
    collectionView = UICollectionView(frame: self.contentView.bounds, collectionViewLayout: layout)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.backgroundColor = .clear
    self.addSubview(collectionView)

    collectionView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant:0).isActive = true
    collectionView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant:0).isActive = true
    collectionView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant:0).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant:0).isActive = true
}

输出用户界面

enter image description here

最佳答案

如果您使用约束,则无需为 Collection View 提供框架。

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) // no need to give frame as you are adding constraints
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
    collectionView.backgroundColor = .clear
    self.contentView.addSubview(collectionView)


    collectionView.leftAnchor.constraint(equalTo: self.contentView.leftAnchor, constant:0).isActive = true
    collectionView.rightAnchor.constraint(equalTo: self.contentView.rightAnchor, constant:0).isActive = true
    collectionView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant:0).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant:0).isActive = true

}

关于ios - UICollectionView 的框架未正确更新为 UITableViewCell subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59854176/

相关文章:

objective-c - iPad 将图像显示得很大 - 但不应该

ios - SpriteKit 场景

ios - tableViewCell 中的约束未正确显示

ios - SKAction scaleBy : duration: SpriteKit

ios - 快速正确使用 UITextField

ios - CIColor 到 UIColor -> CIColor 没有为 UIColor UIExtendedSRGBColorSpace 定义

ios - 在 UITableView 中显示 html 文本

ios - iOS8 中的 UIScrollView 行为不同

ios - Xcode 10、带有 CoreNFC 的 Swift 4 应用程序在 iOS 12 上崩溃审查

swift - 是否有与 C#'s ' nameof( )' function to get a variable or member' s name?