Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient way to customize UIButtons

What is the best way to customize the look of UIButtons in an app? I am working on an app where we programatically update UIButtons to look the way we want. Most of the buttons are created in Interface Builder and we want to execute our customizeButton-method on them in an efficient way. I only see two ways of achieving this:

  1. Create the buttons in IB, assign them to an outlet and in code call customizeButton for each button. Problem: way too much hazzle
  2. Subclass UIButton, in the constructor initWithCoder: call customizeButton. Then use this class for buttons in IB. Problem: allthough this works for buttons created in IB and requires less boilerplate code than point 1, I have read all the warnings against subclassing UIButton because of their class cluster.

Clearly there must be some better way, what is point 3?

like image 266
Olav J Avatar asked Dec 30 '25 22:12

Olav J


1 Answers

Have a look at UIAppearance. It is available since iOS 5 and is especially for customizing the standard UI elements.

For ever class that is customizable is a class method called +appearance: and you can use it like this.

[[UIButton appearance] setBackgroundColor:[UIColor redColor]];

Every button will have now a red background color.

To get started have a look at Ray Wenderlichs Tutorial User Interface Customization in iOS 5 and/or session 114 - Customizing the Appearance of UIKit Controls of the WWDC11 videos.

like image 77
Pfitz Avatar answered Jan 01 '26 14:01

Pfitz



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!