从笔尖创建的 UICollectionViewCell 中的奇怪 UIView

Yar*_*lav 2 nib ios uicollectionview swift

我有一个奇怪的视图出现在我的 UICollectionViewCell 子类中,它具有 2 个图像视图和 1 个按钮的简单结构。

final class ProfileImageCell:UICollectionViewCell {
    static var name: String { return "ProfileImageCell" }
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var anotherImageView: UIImageView!
    func setup(...) { ... } 
    @IBAction func buttonAction(_ sender: UIButton) { ... }
}
Run Code Online (Sandbox Code Playgroud)

在 setup() 方法中,我设置了 imageViews 并传递了一些属性。我不创建任何视图或更改自我子视图。

然后在我的视图控制器中,我像往常一样设置 collectionView。

collectionView.register(UINib(nibName: ProfileImageCell.name, bundle: nil), forCellWithReuseIdentifier: ProfileImageCell.name)
Run Code Online (Sandbox Code Playgroud)

ProfileImageCell.xib

xib 结构

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ProfileImageCell.name, for: indexPath) as! ProfileImageCell
    cell.setup(...)
    return cell
}
Run Code Online (Sandbox Code Playgroud)

这是奇怪的事情发生的时候。我的单元格中有 4 个子视图。即使我在调用后立即停止执行:

(lldb) po cell.subviews
? 4 elements
  - 0 : <UIImageView: 0x1026bac00; frame = (0 0; 375 667); autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x170236dc0>>
  - 1 : <UIImageView: 0x1026bade0; frame = (263 0; 112 112); autoresize = RM+BM; layer = <CALayer: 0x170237160>>
  - 2 : <UIButton: 0x1026ba4d0; frame = (263 0; 112 112); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x170237140>>
  - 3 : <UIView: 0x102732cb0; frame = (0 0; 600 600); gestureRecognizers = <NSArray: 0x17444e430>; layer = <CALayer: 0x174238aa0>>
Run Code Online (Sandbox Code Playgroud)

任何人都知道UIView可能来自哪里?它有一个奇怪的框架(在单元格出现在屏幕上后它不会改变),覆盖了我的所有单元格并且不让任何手势通过按钮。另外,非常有趣的是,为什么它有一个手势识别器?

(lldb) po cell.subviews[3].gestureRecognizers?.first?.description
? Optional<String>
  - some : "<UILongPressGestureRecognizer: 0x10271db10; state = Possible; view = <UIView 0x102732cb0>; target= <(action=_handleMenuGesture:, target=<Application.ProfileImageCell 0x1026ba790>)>>"
Run Code Online (Sandbox Code Playgroud)

xou*_*ini 7

这是contentView单元格的 - 请参阅文档

您的子视图应该直接添加到contentView当前所在的单元格而不是单元格中。发生这种情况是因为您的笔尖是一个UIView不包含该contentView属性的平原。

你需要做的是用一个UICollectionViewCell对象设计你的笔尖,只需将适当的对象拖到界面构建器中:

图片