Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Images
Resources/Images.xcassets/LaunchImage.launchimage
AppDelegate.cs
Buttons.csproj
Buttons.sln
ButtonsViewController.cs
Info.plist
Main.cs
README.md
star-gold45.png
star-gold45@2x.png
star-gold45_sel.png
star-gold45_sel@2x.png
star-grey45.png
star-grey45@2x.png

README.md

id title brief article sdk
F3FAE55E-2FF9-37BB-280C-37987E83D1D4
Use an Image for a Button
This recipe shows you how to create an image button.

Recipe

Buttons have a number of different states, and you can set the button image once for all states or assign different values for each state.

To set the ‘default’ image for a button, call SetImage for UIControlState.Normal and that image will be used for all states:

buttonRect = UIButton.FromType(UIButtonType.System);
buttonRect.SetImage(UIImage.FromFile ("star-gold45.png"), UIControlState.Normal);

Alternatively you can make each state different, as this image shows:

For most button types, only two states – Normal and Disabled – need to be set (the blue highlighted color is automatically applied to the transparent parts of the image):

For the Custom button type you must supply a separate image for the Highlighted state if you wish the button to look different when pressed.

buttonStarCustom = UIButton.FromType(UIButtonType.Custom);
buttonStarCustom.SetImage (UIImage.FromFile ("star-gold45.png"), UIControlState.Normal);
buttonStarCustom.SetImage (UIImage.FromFile ("star-gold45_sel.png"), UIControlState.Highlighted);
buttonStarCustom.SetImage (UIImage.FromFile ("star-grey45.png"), UIControlState.Disabled);

Additional Information

You can also set the different states using Interface Builder by choosing each one from the drop-down-list in the Attribute Inspector (see the Set Button Text recipe for an example using Interface Builder).

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.