How can I change the background image of my UIButton in click event? And refresh it in a few second with the previous image? I mean change it's background image on click and reset it after the click.
[yourButton setImage:[UIImage imageNamed:#"yourNormalImage.png"] forState:UIControlStateNormal];
[yourButton setImage:[UIImage imageNamed:#"yourImageWhenClicking.png"] forState:UIControlStateHighlighted];
put your clickEvent image as highlighted image of your button..
The above answer is not working because it doesn't change the background but the side image of the UIButton, to change the background image use:
[yourButton setBackgroundImage:[UIImage imageNamed:#"yourimage"] forState:UIControlStateNormal];
Related
I've set my UIButton normal state to one image and it's highlighted state to another. The button normal is fine but when I push the button down it sets it to a gray faded version of the image it's supposed to be. In the image below the top grayed out button is being pressed and the other buttons are in their normal state.The buttons are programmatically made.
UIButton *button;
UIImage *bluenormalImage = [UIImage imageNamed:#"bluebuttonnormal.png"];
UIImage *bluepressedImage = [UIImage imageNamed:#"bluebuttonpushed.png"];
[button setBackgroundImage:bluenormalImage forState:UIControlStateNormal];
[button setBackgroundImage:bluepressedImage forState:UIControlStateHighlighted];
OK, I didn't realise the text wasn't part of the image. Do this instead:
From what I can see you need to properly initialise the button as a custom type. The highlight issue occurs when the button type is system.
Change your button variable line to the following:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
If you initialise it later, you can do the following before you set the background image.
UIButton *button
button = [UIButton buttonWithType:UIButtonTypeCustom];
I'm facing a problem in xcode, in one of my view controllers i have two buttons, when one of them is clicked (appleBtn) the second button's (topMenu1Btn) image should change
I created an outlet for both buttons, an action for the applebtn when clicked
- (IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView = [UIImage imageNamed:#"Apple.jpg"]
}
the thing is the imageview is read-only
is there anyway to do this?
- (IBAction)HandleBtnClick:(id)sender
{
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
}
You could try this to set the image for the button.
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you want the background image use setBackgroundImage: instead
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
Or
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you use the second method then make sure that the frame of image matches with the button
Make sure that Button type is Custom
(IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView.image = [UIImage imageNamed:#"Apple.jpg"]
}
You can't modify the imageview, but you can modify the image in it ;)
Okay so I have an app I'm building that is almost complete. I'm just having one problem, when I press my button it makes a sound and the button's image changes. However the image only changes when it is being touched or "highlighted" and I would like the buttons image to remain changed through the duration of the sound effect then after the sound effect I would like it to revert back to the original image. Is there anyway I can set up a UIButton "set time" or something for the "highlighted" option?
I feel so embarrassed sometimes, because I seem to get tripped up by these most trivial things, when I handle the core coding exceptionally well and near finish a full app in a days time, but this is my first app and I'm still a newbie to XCode. I really appreciate this community's help any answer that pushes me forward is always appreciated!
I further apologize for my questions formatting I typed this on my iPhone I hope it's not too awkward or lacking in detail. If anyone needs more detail just ask!
Put an image in button's attribute selected image in the XIB and when pressed
[(UIButton *)[self.view viewWithTag:buttonTag] setSelected:Yes];
It may be helpful
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateHighlighted];
If this not succeed then use one of the state from the below
UIControlStateNormal ,
UIControlStateHighlighted ,
UIControlStateDisabled ,
UIControlStateSelected ,
UIControlStateApplication ,
UIControlStateReserved .
1.You dont want any reflection of image at highlighted and selected state. so you just put same image for all state like
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateHighlighted];
[btnClear setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateSelected];
2.Now if you want image change at sound stop time than make
[btnClear setBackgroundImage:[UIImage imageNamed:#"red_button.png"]
forState:UIControlStateNormal];
[btnClear setBackgroundImage:[UIImage imageNamed:#"red_button.png"]
forState:UIControlStateHighlighted];
[btnClear setBackgroundImage:[UIImage imageNamed:#"red_button.png"]
forState:UIControlStateSelected];
And again on button click you just toggle image with first one.
you can toggle background image on sound effect by using some delegate methods of the sound player. When you finish playing sound, change the back image.
e.g.
-(void) musicDidFinishPlaying
{
[btn setBackgroundImage:[UIImage imageNamed:#"normalImage.png"]
forState:UIControlStateNormal];
}
-(IBAction) buttonClicked:(id)sender
{
[btn setBackgroundImage:[UIImage imageNamed:#"soundImage.png"]
forState:UIControlStateNormal];
}
I have a custom UIButton with 2 background images:
in Default State: default.png
in Selected State: selected.png
set in the Xib File.
This is gets invoked on button touch down:
-(IBAction)numberSelected:(id)sender{
NSLog(#"Button pressed %#",[sender currentTitle]);
UIButton* button = (UIButton*)sender;
button.selected = !button.selected;
[button release];
}
The wrong behavior is this:
I press the button, the background image switches to selected.png (correct), I press it again, and no background image is shown. I press it another time, the app crashes.
I think you forgot to set : UIControlStateSelected adding the image.
[button setImage:[UIImage imageNamed:#"Selected.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
Edit:
Why do you make [button release]; Of course it will crash the second time.
No need to release the button there. Remove
[button release];
[button release];
This statement has to be removed!
board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.
when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}
I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.
Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().