This recipe shows you how to create the five different types of UIButton.
There are six different types of UIButton, enumerated in UIButtonType:
This screenshot shows the different types, along with two different custom buttons: one with an image and one with formatted text.
The sample code includes two different ViewControllers: one using an XIB file and the other creating all the controls in C# code.
- To create a button in code, use the UIButton.FromType static method like this:
var buttonRect = UIButton.FromType(UIButtonType.RoundedRect);
- If a text label is required, it is set using the SetTitle method:
buttonRect.SetTitle ("Button!", UIControlState.Normal);
If you use Interface Builder to create your user interface then you can choose the button Type in the Attributes Inspector and set the other properties appropriately.
Buttons have multiple states (including Disabled, Selected and Highlighted). In the example above the properties are only set for the Normal state, however properties such as Title and TitleColor can be set independently for each state. See the Set Button Text recipe for an example.