Background UIButton VS UIButton imageview.view

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

Thanks!

+4
source share
4 answers

myButton.imageView.image is a readonly property and therefore cannot be changed as follows. whereas setBackgroundImage is a method that sets the background correctly.

// EDIT

the setImage method for UIButton REQUIRES two arguments,

 setImage:(UIImage *) forState:(UIControlState) 

thus, using the above method works fine and the other does not.

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

Whenever you call the current ImageView, it returns the selected status background image

Various parameters are highlighted / selected / disabled

+2
source

imageView of UIButton does not indicate a background image. This is the kind of image that appears next to the name. Thus, imageView will always be created if necessary. If you set yourButton.imageView.image before creating, there is nothing to display, because imageView is null.

Thus, UIButton has states ( UIControlState ) for managing, creating, or deleting both the background image and imageView when the states change. (also name)

Please see the UIButton documentation: 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: 
+2
source

@ The right guy - UIButton has a readonly property for its image. Thus, you cannot directly set the image by referring to the imageView image.

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

Use a convenient method

 [UIButtonInstance setImage:imageName forState:buttonState]; 
+1
source

Source: https://habr.com/ru/post/1394387/


All Articles