Joris Kluivers

I like to build software.

CALayer Based Button Styling

Hackernews recently had a link on its homepage to an article comparing different ways to customize UIButton. One of the customizations used CALayers to change the look of the button.

Using CALayer is my favourite way to implement UIs. However the implementation in the article was a bit odd, and possibly educating readers wrongly. To show a correct implementation I submitted a pull request with three improvements:

  • Since there was no Quartz drawing going on I removed the use of drawRect:. Layers are initialized in the init... methods.
  • Using inherited methods from UIControl instead of doing event handling by hand.
  • Disabled implicit layer animations to be more in line with the other implementations that didn’t have animations either.

See the diff with my changes of the accepted pull request for more details. The article has also been updated since to reflect my suggested changes.