Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange UIView in a UICollectionViewCell created from nib

I have a weird view appearing inside my UICollectionViewCell subclass with a straightforward structure of 2 imageViews and 1 button.

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) { ... }
}

In setup() method I set up imageViews and pass around a few properties. I do not create any view or change self subviews.

Then in my viewcontroller I setup collectionView as usual.

collectionView.register(UINib(nibName: ProfileImageCell.name, bundle: nil), forCellWithReuseIdentifier: ProfileImageCell.name)

ProfileImageCell.xib

xib structure

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

This is when strange things happen. I get 4 subviews in my cell. Even if I stop execution right after calling:

(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>>

Anyone has any idea where does that UIView might come from? It has a weird frame (which doesn't change after the cell appears on screen), covering all of my cell and not letting any gestures through to the button. Also, very interestingly, why does it have a gesture recognizer?

(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>)>>"

1 Answers

It's the contentView of the cell – see the documentation.

Your subviews should be added to the contentView rather than the cell directly, where they are currently. This has happened because your nib is a plain UIView which doesn't contain the contentView property.

What you need to do is design your nib with a UICollectionViewCell object, by simply dragging the appropriate object into the interface builder:

image

like image 117
xoudini Avatar answered Oct 18 '25 06:10

xoudini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!