Search code examples
iosswiftuikituicollectionviewuicollectionviewlayout

how to trigger drawrect after rotation for a custom UICollectionView?


How does one trigger a redraw (i.e. calling drawrect) after a device rotation for a custom UICollectionView, which has customer UICollectionViewCell and UICollectionViewLayout?

Extra of function from custom UICollectionViewCell:

    override func drawRect(rect: CGRect) {
        let path = UIBezierPath(roundedRect: self.bounds.insetBy(dx: 1, dy: 1), cornerRadius: 50)
        path.lineWidth = 3
        UIColor.blueColor().setStroke()
        path.stroke()
    }

Before Rotation

Before rotation

After Rotation (layout was updated, noting label moved, however oval drawn in drawRect wasn't, and logging shows that "drawRect" wasn't called upon rotation)

After rotation


Solution

  • You need to call cell.setNeedsDisplay() after rotation. This will cause the cell's drawRect: method to be triggered again.