Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between UIButton methods that set image

Tags:

ios

uibutton

Please tell me what's the difference between two UIButton methods:

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

and

- (void)setImage:(UIImage *)image forState:(UIControlState)state

Apple documentation says nothing about it.

like image 478
Stas Avatar asked Jan 05 '12 14:01

Stas


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage . Save this answer.

What is a Uibutton?

A control that executes your custom code in response to user interactions.

What is UIImage?

An object that manages image data in your app.

How do I center a button in Swift?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen.


1 Answers

setBackgroundImage sets the background image of the button. Typically you use this in conjunction with a button of type "Custom" to use the background image as a nice shape and background for the button (say an image representing a rectangle with rounded corners and filled with a nice gradient)

setImage adds an image/icon next to the title of the button, (all of which, image and title, are overlayed over the background image, of course, that's why it's called "background" after all). If you set some title to you button too, the image will be drawn next to the title and its position will depend on the title length (and also on the titleContentInsets / imageContentInsets properties to adjust this position)

Just try it in Interface Builder (set an image for both, play around by setting a title, etc) you will understand it easier.

like image 76
AliSoftware Avatar answered Sep 28 '22 01:09

AliSoftware