When you are using Interface Builder to create your buttons there is just a few properties that we can change for different
So, that brings us some restrictions if we need to change the background color of the highlighted state. I have seen people creating an image of a button relatively simple, but one image for the normal and the other for the highlighted state or some other properties. I confess that it was my approach for a short while, until I had to deal with a bunch of buttons that the only thing that changed was the text, and sometimes an icon embeded in the button.
For that reason I implemented a class to deal with the background of the highlighted state and some border features.
How to use it?
First you have to add Catfish as a dependency to your project, inside of it there is a class
CFUIButton. It has methods to:
1 2 3 4 5 6 7 8 9
If you are using Interface Builder, the next step is to set
CFUIButton as a custom class for your button.
Cool, and now set your
CFUIButton type as custom.
But if you are creating your button programmatically, you just have to extend
1 2 3
Now that you have everying set, lets play with it.
Setting border color
There are two different ways to change the border color of the
CFUIButton. The first one is for all of the
CFUIButton in your application and the second is for a specific
CFUIButton instance. The default color for all of the
[UIColor colorWithWhite:1.0 alpha:0.4].
AppDelegate.m and add the following code in the method
You should have something like that:
1 2 3 4 5 6
First you have to create a class for your
ViewController, for this example I am using
MainViewController, after that you have to reference your
CFUIButton with your
MainViewController.h and set the instance name
1 2 3 4 5 6 7
Now, open the class
MainViewController.m and add the following line in your
- (void)viewDidLoad method:
You also can create button with corner:
Since that you have your instance variable, in the
- (void)viewDidLoad method, add the following line:
You can also remove the border if you just want to use the benefit of the highlighted state color.
You can change the text color for the highlighted state through the Interface Builder, but to change the background color, it becomes a little bit more complicated, so if you want to have something like that:
You can use:
1 2 3 4 5 6
Download the project sample.