SvenTiigi / WhatsNewKit

Showcase your awesome new app features 📱
https://sventiigi.github.io/WhatsNewKit/
MIT License
3.87k stars 190 forks source link

Add checks to prevent invalid context error. #40

Closed GetToSet closed 4 years ago

GetToSet commented 4 years ago

I found a weird error log regarding invalid CGContext when using this framework. Error regarding invalid context

After some investigation, I found its cause:

CompletionButton is redrawing its background image at traitCollectionDidChange, which is called before layoutSubviews. At early initialization stage, the button may have empty frame with both height and width set to 0, resulting "invalid context error" when drawing background image.

This commit adds extra check to prevent creating context when frame is empty, delaying the creation of background image to layoutSubviews call.

SvenTiigi commented 4 years ago

Hey @GetToSet,

thanks for the investigation and fixing the bug. Great work 👍

SvenTiigi commented 4 years ago

Additionally, I've just tweaked the implementation a little bit (see https://github.com/SvenTiigi/WhatsNewKit/commit/eb588f26dd6410333a61d8e069a24d2c64eaef89)

From

if size.width == 0 && size.height == 0 {
    return nil
}

To

guard size != .zero else {
    return nil
}