How to change background image on a button , iOS? - objective-c

I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that?
I tried to put into the action method of the button something like this:
snapshotCheckbox.image = [UIImage imageNamed:#"snapshot.png"];
but i guess this method is for UImageViews. How can i do the same thing for a button?
Thank you very much for reading my post :D

you can set the image for a given state of the button in the viewDidLoad:
[myButton setBackgroundImage:[UIImage imageNamed:#"myBackgroundImage.png"] forState:UIControlStateHighlighted];

A button has two properties image and backgroundImage..
For setting image use
button.currentImage = image (or)
[button setImage:image ForState:UIControlStateNormal];
For setting backgroundImage use
button.currentBackgroundImage = image (or)
[button setBackgroundImage:image ForState:UIControlStateNormal];

First set the tybe of the button to
button = [UIButton buttonWithType :UIButtonTypeCustom];
then use
[button setImage:[UIImage imageNamed:#"imagename.type"] ForState:UIControlStateNormal];

One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom".
This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.
Hope this helps.

You have the setBackgroundImage:forState: method on the button object. See Setting an image for a UIButton in code for more information (seconds answer).
Also, UIButtons automatically change the image when pressed if you set an image for the UIControlStateHighlighted state (though only as long as the user keeps pressing on the button).

Setbutton
if u need image while clicking
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateHighlighted];
or if u want image after Selecting it
[snapshotCheckbox setImage:[UIImage imageNamed:#"snapshot.png"] forState:UIControlStateSelected];
and on that onclick function mention
as
snapshotCheckbox.Selected=YES;

To set an image on a button, just press the button you want an image to in Main.storyboard, then, in the utilities bar to the right, press the attributes inspector and set the background to the image you want! Make sure you have the picture you want in the supporting files to the left.

Related

Change background color of UIButton while highlighted

Is there any way to change the background of a uibutton while it is highlighted and then have it revert back to original state?
You can set an image in the utilities panel at the right-side of your screen.
Or use this code:
[button setImage:[UIImage imageNamed:#"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];

NSButton with clickable image and text

I've created an NSButton (see code below). The button displays the way I want (borderless, with image above text). If I click the text portion of the button the button will press down and call it's action. If I click the image portion of the button then absolutely nothing happens. How can I make the image portion of the NSButton clickable? I'm sure I can subclass the NSButtonCell to make this work, but I don't understand why that would be necessary.
NSButton *button = [[NSButton alloc] initWithFrame:NSZeroRect];
[button setBezelStyle:NSRegularSquareBezelStyle];
[button setBordered:YES];
[button setImagePosition:NSImageAbove];
[button setButtonType:NSMomentaryChangeButton];
[button setImage:[NSImage imageNamed:#"myImage"]];
[button setTitle:#"Button Title"];
[button setTarget:self];
[button setAction:#selector(buttonAction:)];
The image on the button itself. No need to make it clickable. Your click event will be caught by buttonAction.
You only need to show the animation that image got clicked. For that, you need another inverse image. And track the mouse down event, when you click down show the other image, when you release mouse (mouse up) show the default image.
This will give the feel of click on the image.
Maybe some other view overlaps your button's image? In code above, of course if you provide appropriate button frame, all works fine (for both, bordered and borderless button). Try it in clean project.

Open UIImagePicker when tapping on UIImageView

I need to allow the user to add photo either from source type camera or photo library when the user taps on the UIImageview.
I don't want to present a uiimagepicker controller by clicking on a UIButton, I want to allow the user to add it by tapping on the imageview.
How can I achieve this? Should I add the button the imageview, and when a tap occurs, present an action sheet with buttons camera and photo library and when the user selects one of the 2 buttons from action sheet allowing him to add photo to the uiimageview?
You can still use a UIButton, just have that button's view as the image you want.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,100,100); //edit for you own frame
UIImage *img = [UIImage imageNamed:#"myimage.png"];
[button setImage:img forState:UIControlStateNormal];
This will allow you to use an image as your button.
#Amit Shah has the right answer - you should use a button.
But if for some reason you MUST use a UIImageView (and it doesn't sound like you do) - you can subclass UIImageView and override the touchesBegan/touchesEnded methods:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIResponder_Class/Reference/Reference.html

How to set the button image for UIControlStateSelected

Hi I want to change the background of my button after I press the button i.e. the button should not change back to the previous image.
However, I am not able to do so. My code as follows. Can anyone advise how I can change the image to "custom_button_highlight" after I click on the button?
UIImage *buttonImageNormal = [UIImage imageNamed:#"custom_button"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:#"custom_button_highlight"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateHighlighted];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateSelected];
METHOD1
I see that you are using code to generate UIButton. It's easier to use Interface builder. Try this - In Interface builder (Xcode4), create the button & open the right sidebar. After that select the button, & the type of button as custom. After that in state config, there are various states, for each state you can set an image in image. Hope this helps...
METHOD2
If you want to use code to generate UIButton (it beats me why) then you seem to be doing the right thing. Are you sure the images are there in your bundle? You did not include the image extension (like .jpg etc.)
You mentioned, that you don't want the image to change back...
then instead of setting it for the states highlighted or selected, you have to set it for the state UIControlStateNormal.
But since you need it after it's been pressed, you have to catch the event that your button has been pressed and set the background image in there.
Hope it helped ;)
[_buttonConfirm setImage:[UIImage imageNamed:#"s.png"] forState:UIControlStateSelected ];
... does what you want programatically, not the background image. But I think you should follow Srikar's answer and use interface builder if you can.

How to change UIButton's background image while clicking it?

I have a UIButton in each of my tableview's cells. I want to glow the uibutton while click action happends. I set an image as its background image using the code
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
The image.png is little bit darker image. Because i want to show a click effect by showing a glowed image.
Now shall i glow the image.png by code while clicking happends? Or should i create a new glowing image(say image_glow.png) with the same dimension and replace the previous one?
Whatever, please explain how to implement this....
The best way is to use multiple images.
You can set different image to different state of a UIButton by using setImage method.
As i see, you have set image only for UIControlStateNormal state, which makes all other states of a button to use the same image.
reference : UIButton States
Hope this helps.