Buttons in Xamarin.iOS

In iOS, the UIButton class represents a button control.

A button's properties can be modified either programmatically or with Xcode's Interface Builder.

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 property:

    • 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 for button state:

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

    The button's state types are listed below:

    • UIControlState.Normal
    • UIControlState.Highlighted
    • UIControlState.Disabled
    • UIControlState.Selected
    • UIControlState.Focused
    • UIControlState.Application
    • UIControlState.Reserved

    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:

myButton.TouchUpInside += (sender, e) => {
    DoSomething();
};

Note

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

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.

Note

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:

myButton.SetTitleColor(UIColor.White, UIControlState.Normal);
myButton.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:

myButton.SetImage (UIImage.FromBundle ("Buttons/MagicWand.png"), UIControlState.Normal);
myButton.SetImage (UIImage.FromBundle ("Buttons/MagicWand_Highlight.png"), UIControlState.Highlighted);
myButton.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.