Create Different Types of Buttons

PDF for offline use
Sample Code:
Related Articles:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows you how to create the six different types of UIButton.

Recipe

There are six different types of UIButton, enumerated in UIButtonType:

System

DetailDisclosure

InfoDark

InfoLight

ContactAdd

Custom

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.FromType static method like this:
var buttonRect = UIButton.FromType(UIButtonType.System);
  • 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.

Additional Information

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.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.