UIButton background VS UIButton imageview.view

10,497

Solution 1

myButton.imageView.image is a readonly property and thus can't be change like this. whereas setBackgroundImage is a method which sets the background properly.

//EDIT

the method setImage for UIButton REQUIRES two arguments,

setImage:(UIImage *) forState:(UIControlState)

thus using the above mentioned method works fine and other doesn't.

Solution 2

[myButton setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[myButton setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateHighlighted];

Whenever you call for current ImageView, it will return the selected state background image

the Different stats are highlighted/selected/disabled

Solution 3

imageView of UIButton does not indicate background image. It is the view of image that shows next to title. Thus imageView will always be created if necessary. If you set yourButton.imageView.image before creation, there is nothing to display becasue imageView is nil.

So UIButton has states (UIControlState) to control, create or delete both background image view and imageView when changes between states. (also title)

Please take a look at documentation of UIButton : http://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIButton_Class/UIButton/UIButton.html

you will see two different methods for images

– setBackgroundImage:forState:
– setImage:forState:

Solution 4

@Tough Guy is correct - A UIButton has a readonly property for its imageView. So you cannot directly set the image by accessing image of imageView.

@property(nonatomic,readonly,retain) UIImageView *imageView   __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

Use the convenient method

[UIButtonInstance setImage:imageName forState:buttonState];
Share:
10,497
Alaa Eldin
Author by

Alaa Eldin

I like being iPhone developer, don't ask why, I don't know :)

Updated on June 04, 2022

Comments

  • Alaa Eldin
    Alaa Eldin almost 2 years

    What is the difference between UIButton background and UIButton imageview.view I tried myButton.imageView.image=[UIImage imageNamed:@"image.png"]; then nothing happens, but when I use [myButton setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal]; button's background changes.

    Thanks !

  • Prashant Bhayani
    Prashant Bhayani over 12 years
    @Tough Guy if it is read only property then it will give an error.
  • Matthias Bauch
    Matthias Bauch over 12 years
    the imageView is readonly. That means you can't change the imageView of the button. But the image property of the imageView is not readonly, so you can set the image of the imageView without any problems.
  • Dhilip
    Dhilip over 12 years
    @MatthiasBauch Since ImageView is a readonly property, you cannot assign or allocate imageView for the UIButton. Only UIButton allocates UIImageView during the [UIButtonInstance setImage:imageName forState:buttonState]; So you can never assign an image to UIButtonInstance.imageView
  • Alaa Eldin
    Alaa Eldin about 12 years
    Actually, I tried what you said, it seems logical, but it doesn't give me "the selected state background image", have you tried it ?
  • bmeulmeester
    bmeulmeester over 9 years
    @Dhilip, this is in fact not true. The ImageView is readonly, but like Matthias Bauch said, it's image property is not and thus can be changed by using UIButtonInstance.imageView.image or even [UIButtonInstance.imageView setImage:image];.