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. |
|
|
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);
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).