Change button image on click event - ios7

I want to change button image. As when i click on the button it will change it's image and after the click the button will return in its normal form.I want to know how to implement this? Please help me.

UIImage *buttonImage = [UIImage imageNamed:#"image.png"];
[sender setImage:buttonImage forState:UIControlStateHighlighted];
UIImage *buttonImage1 = [UIImage imageNamed:#"image.png"];
[sender setImage:buttonImage1 forState:UIControlStateNormal];

You can customize the "highlighted" state of your button right in Interface Builder, or use this code:
[self.button setImage:[UIImage imageNamed:#"normal"] forState:UIControlStateNormal];
[self.button setImage:[UIImage imageNamed:#"pressed"] forState:UIControlStateHighlighted];

first fix the Backgroudimage is #"image1.jpeg" in xib, then
- (IBAction)mybutton:(id)sender
{
[_buttonrefrence setBackgroundImage:[UIImage imageNamed:#"download.jpeg"] forState:UIControlStateHighlighted];
}

Related

Event on an Image in iOS

I am pretty new to iOS development, so this may seem to be a stupid question.. In my xib file, i have taken a UIImageView that will show an image when the page is loaded. But when a user taps on an image, I want the next view to be loaded.. But I am not able to do that because we cannot have a delegate not a target-action mechanism for a UIImage view.. Many places, I found a workaround.. using a button and then placing an image on it, and then using it.. but I want the first way to work.. Please help..
use UITapGestureRecognizer ,and Don't forget to set imageView.userInteractionEnabled = YES; because by default image not having UserInteraction.
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
}
(OR) If you want
take button with BackGround image.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(270,10,30,30);
[button setBackgroundImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
Instead of Using UIImageView, you can use UIButton.
UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[myBtn setBackgroundImage:[UIImage imageNamed:#"your_image_Name"] forState:UIControlStateNormal];
[myBtn setBackgroundImage:[UIImage #"your_image_Name"] forState:UIControlStateHighlighted];
[myBtn addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
also write button action:
-(void)buttonAction:(id)sender {
// Do whatever you want
}
In Case of XIB, Just use UIButton instead of UIImageView and set Background Image for both state Normal & Highlighted as you want. Rest you can manage button's action from XIB.
You can also add a fully transparent UIButton on top of your UIImageView... It should be preferred as each element has a clean semantics.

UIButton setImage is not working

I am trying to set the UIImage for a UIButton via the following code, but nothing happens. I am setting these inside viewDidLoad. The only way it shows up is if I use storyboard and set the image that way.
- (void)viewDidLoad
{
...
[self.locateMeButton setFrame:CGRectMake(11, 11, 46, 46)];
[self.locateMeButton setImage:[UIImage imageNamed:#"locateme_disabled.png"] forState:UIControlStateDisabled];
[self.locateMeButton setImage:[UIImage imageNamed:#"locateme_active.png"] forState:UIControlStateHighlighted];
[self.locateMeButton setBackgroundImage:[UIImage imageNamed:#"locateme_normal.png"] forState:UIControlStateNormal];
...
}
UIButton *aboutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[aboutBtn addTarget:self action:#selector(showAboutPage) forControlEvents:UIControlEventTouchUpInside];
[aboutBtn setTitle:#"About Me" forState:UIControlStateNormal];
[aboutBtn setFrame:CGRectMake(80.0, 120.0, 162.0, 42.0)];
[aboutBtn setBackgroundImage:[UIImage imageNamed:#"locateme_normal.png"] forState:UIControlStateNormal];
[aboutBtn setBackgroundImage:[UIImage imageNamed:#"locateme_disabled.png"] forState:UIControlStateHighlighted];
I had a similar problem. It turned out in my case that I was recreating the toolbar so the wrong toolbar object was being used. (Was trying to access a button on the wrong toolbar). Basically it seems a good idea to just check systematically that your methods are acting on all the receivers you expect and are being passed parameter values that you would expect.

UIButton show selected state on tap

How do I show the selected state for the UIButton on tap?
I want it so that when people tap on the uibutton it shows the selected state with the hover that I have?
Right now they have to hold the button to see the hover.
[t1 setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: #"hover.png"]] forState:UIControlStateSelected];
I want to show the hover image on tap also.
You need to set the selected state for the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setImage:[UIImage imageNamed:#"hover.png"] forState:UIControlStateSelected];
To show a hoover image while the button is pressed you need to set the state property to UIControlStateHighlighted
I had the same problem - my button was highlighted when pressed (tap+hold) and wasn't - when simply tapped. Here is my code:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image =[UIImage imageNamed:#"btn"];
UIImage *imageSelected =[UIImage imageNamed:#"btn_tap"];
[btn setBackgroundImage:transImage forState:UIControlStateNormal];
[btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
-(void)btnPressedWithDelay{
//your code for btn action
}
- (void) action:(UIButton *)sender{
[self performSelector:#selector(btnPressedWithDelay) withObject:nil afterDelay:0.1];
}
Did it work for you?

UIImage Buttons (Rounded Rect Button)

I want to be able to change the image of any of the buttons upon the user pressing the button and then when they release, the image that was there before is put back.
So basically when they press their finger on the button, the image changes and when they release, the original image I specified in IB is put back.
How can I do this? Here is the code I have so far.
- (IBAction)btnOne:(id)sender {
result.text = [self appendresult:#"1"];
//Changes the button image upon touch
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
}
Put your code...
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
in viewDidLoad instead of in the IBAction, then just make sure that your button in Interface Builder is properly linked to the IBOutlet you created for it.

how to make button round with image background in IPhone?

I have a round button, but i will make it have a backgroud with image.
but when i do it, by setting the (what) property to an image, the button becomes rectangle because the image is a rectangle. How do I keep the button round?
Simply by doing this
#import <QuartzCore/QuartzCore.h>
myButton.layer.cornerRadius = 8;
Tested Code:
.h
-(void)vijayWithApple;
.m
-(void)vijayWithApple{
NSLog(#"vijayWithApple Called");
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Micky.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(vijayWithApple) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(135.0, 180.0, 40.0, 40.0);//width and height should be same value
button.clipsToBounds = YES;
button.layer.cornerRadius = 20;//half of the width
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
[self.view addSubview:button];
Restut
Best solution I have found especially for UITableCell. I put the code in awakeFromNib and worked right off the bat.