Search code examples
iphoneiosuibuttondelaysetbackground

Delay when updating the background of a UIButton


I'm using setBackgroundImage:forState: to update the background of a UIButton. The background is updated but only after a few seconds. There is obviously some refresh cycle at work here but I can't find exactly what the problem is. I've tried to play with setNeedsRefresh and setNeedsDisplay without any result.

How can I change the UIButton background instantaneously ?

Thanks


Solution

  • Strange delays like this are usually caused when you try and perform UIKit operations on a background thread (e.g. in a delegate callback). Try this:

    dispatch_async(dispatch_get_main_queue(), ^{
        // set your button background here
    });