Buttons in Xamarin.iOS

In iOS, the UIButton class represents a button control.

A button's properties can be modified either programmatically or with the Properties Pad of the iOS Designer:

The Properties Pad of the iOS Designer

Creating a button programmatically

A UIButton can be created with only a few lines of code.

  • Instantiate a button and specify its type:

    UIButton myButton = new UIButton(UIButtonType.System);

    The button's type is specified by a UIButtonType:

    • UIButtonType.System - A general-purpose button
    • UIButtonType.DetailDisclosure - Indicates the availability of detailed information, usually about a specific item in a table
    • UIButtonType.InfoDark - Indicates the availability of configuration information; dark-colored
    • UIButtonType.InfoLight - Indicates the availability of configuration information; light-colored
    • UIButtonType..AddContact - Indicates that a contact can be added
    • UIButtonType.Custom - Customizable button

    For more information about the different button types, take a look at:

  • Define the button's size and position:

    myButton.Frame = new CGRect(25, 25, 300, 150);
  • Set the button's text. Use the SetTitle method, which requires the text and a UIControlState value:

    myButton.SetTitle("Hello, World!", UIControlState.Normal);

    For more information about styling a button and setting its text, refer to:

Handling a button tap

To respond to a button tap, provide a handler for the button's TouchUpInside event:

button.TouchUpInside += (sender, e) => {


TouchUpInside is not the only available button event. UIButton is a child class of UIControl, which defines many different events.

Using the iOS Designer to specify button event handlers

Use the Events tab of the Properties Pad to specify event handlers for a button's various events.

For the appropriate event, either type the name of a new event handler or select one from the list. Doing this will create an event handler in the code for the button's view controller.

Events tab of the Properties Pad

Styling a button

UIButton controls can exist in a number of different states, each specified by a UIControlState value – Normal, Disabled, Focused, Highlighted, etc. Each state can be given a unique style, specified programmatically or with the iOS Designer.


For a complete list of all UIControlState values, take a look at the UIKit.UIControlState enumeration documentation.

For example, to set the title color and shadow color for UIControlState.Normal:

button.SetTitleColor(UIColor.White, UIControlState.Normal);
button.SetTitleShadowColor(UIColor.Black, UIControlState.Normal);

The following code sets the button title to an attributed (stylized) string for UIControlState.Normal and UIControlState.Highlighted:

var normalAttributedTitle = new NSAttributedString(buttonTitle, foregroundColor: UIColor.Blue, strikethroughStyle: NSUnderlineStyle.Single);
myButton.SetAttributedTitle(normalAttributedTitle, UIControlState.Normal);

var highlightedAttributedTitle = new NSAttributedString(buttonTitle, foregroundColor: UIColor.Green, strikethroughStyle: NSUnderlineStyle.Thick);
myButton.SetAttributedTitle(highlightedAttributedTitle, UIControlState.Highlighted);

Custom button types

Buttons with a UIButtonType of Custom have no default styles. However, it's possible to configure the button's appearance by setting an image for its different states:

button4.SetImage (UIImage.FromBundle ("Buttons/MagicWand.png"), UIControlState.Normal);
button4.SetImage (UIImage.FromBundle ("Buttons/MagicWand_Highlight.png"), UIControlState.Highlighted);
button4.SetImage (UIImage.FromBundle ("Buttons/MagicWand_On.png"), UIControlState.Selected);

Depending on whether the user is touching the button or not, it will render as one of the following images (UIControlState.Normal, UIControlState.Highlighted and UIControlState.Selected states, respectively):

UIControlState.Normal UIControlState.Highlighted UIControlState.Selected

For more information about working with custom buttons, refer to the Use an image for a button recipe.