Create Different Types of Buttons
This recipe shows you how to create the six 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. There is no real visual difference between some of these button types in iOS 7 and later.
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.FromTypestatic method like this:
var buttonRect = UIButton.FromType(UIButtonType.System);
- If a text label is required, it is set using the
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
TitleColor can be set independently for each state.
See the Set Button Text recipe for an example.