Temporarily change a UIButtons image - iphone

I have a UIButton which I assign an image in IB.
I then change the image in the code, but I only want this to be temporary.
How does one return the button to the image set in IB or is there a way to revert back after doing the temporary switch?
Thanks

If you want to revert back to the previous image you would have to store a reference to it somewhere:
// prevImage is an ivar UIImage *
prevImage = [[myButton imageForState:UIControlStateNormal] retain];
// Change to the new image
...
// Later: revert back to prevImage
[myButton setImage:prevImage forState:UIControlStateNormal];
[prevImage release];
prevImage = nil;

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"image3.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"image4.png"] forState:UIControlStateDisabled];
You can set your own custom images for different states. when you tap on it, the corresponding images will appear on your button.

Related

Unable to set Title of UIButton

I am new to iPhone,
xpos=30;
ypos=40;
for(int i=0;i<[MyArray count];i++)
{
if(i!=0)
{
if (i%4==0)
{
ypos+=180;
xpos=30;
}
else
{
xpos+=200;
}
}
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(xpos, ypos, 110.0, 130.0);
[button setImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal];
[button setTitleEdgeInsets:UIEdgeInsetsMake(ypos, xpos, 0.0, 0.0)];
[button setTitle:[NSString stringWithFormat:#"%#", [MyArray objectAtIndex:i]] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[self.view addSubview:button];
}
i am able to see button but unable to see my text on the UIButton.
Any help will be appreciated.
Please set buttons background image.You have set buttons image.
Please use this:
[button setBackgroundImage:[UIImage imageNamed:#"blue_button.png"]
forState:UIControlStateNormal];
For Wrapping button title User:
[button setTitle:#"Test button name 123" forState:UIControlStateNormal];
button.titleLabel.numberOfLines=2;
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
You can't see the text because you have used setImage: function of button. At a time you can display image or button on text but you can add image as a background so you can see both image and tittle. In short set image as a background.
[btn setBackgroundImage:[UIImage imageNamed:#"imageName"] forState:UIControlStateNormal];
Please also remove the line of setting inset.
Rather then setting
[button setImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal]; // This will not allow you to set Button title.
You have to use :
[button setBackgroundImage: [UIImage imageNamed:#"ibook2.png"] forState:UIControlStateNormal]; // This will allow you to set title along background button image.
if you image is too big then it will only show the image and text get truncated due to image as text will be visible after image, try using setBackground image or try small resolution image or setImageEdgeInsets .

UIButton : Background Image setting Issue when button state is highlighted?

I have an UIButton, I need to change button's image when button is tapped. Button's background images are set in ViewDidLoad method. Pls refer images attached on this thread.
When I tap on button first time, button property will be changed to selected and "arrow_right.png" image will be set to our Button.
2.On second tap, I observed that when button state is highlighted, But it's background image is not setting. You can see some blur effect instead.
My concern is I saw while toggling UIButton, image is not getting set for highlighted state. when button's state changing from Selected to Normal.
Is it a bug or my mistake?
Thank you.
- (void)viewDidLoad
{
[super viewDidLoad];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_normal.png"] forState:UIControlStateNormal];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateSelected];
[testButton setBackgroundImage:[UIImage imageNamed:#"btn_pressed.png"] forState:UIControlStateHighlighted];
}
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
button.selected = !button.selected;
if (button.selected)
{
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_right.png"] forState:UIControlStateSelected];
}
else
{
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:#"arrow_left.png"] forState:UIControlStateSelected];
}
}
try this
- (IBAction)buttonPressed:(id)sender
{
UIButton *button = (UIButton*)sender;
if(button.selected) {
[button setSelected:NO]
} else {
[button setSelected:YES]
}
}
i diddn't understand why you need to set image again, all you need to do is set its selected property , button will behave as you expected

UIButton setTitleColor and setImage not working

I am using the StoryBoard feature for my iPhone project. In the storyboard, I have a view controller with a table view using prototype cells. Inside the prototype cell, I have a UIButton (rounded rectangle) hooked up to a sub-classed UIButton class. The tableview gets its data from an array, and the data is printed out in cellForRowAtIndexPath.
Inside cellForRowAtIndexPath, I conditionally modify the cell's appearance. This is when I start getting problems. I am able to successfully change the text of the button. However, if I attempt to change the image of the button, using:
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
the image is changed, but the text disappears.
If I attempt to change the text color of the button, using:
[cell.myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal]
Nothing happens whatsoever. The color remains the same (unless I also try to change the image, in which case it completely disappears).
I am sensing I am doing something fundamentally wrong here -- do you have any suggestions?
use
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
to change image with text left
use
btn.titleLabel.textColor = yourcolor;
to change text color
Define myButton as below and check.This may be the wrong in your code.The code is:
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
This is newly editted part:
[myButton setBackgroundImage:yourimage forState:UIControlStateNormal];
[myButton setTitleColor:yourcolor forState:UIControlStateNormal];
This is the way to set different properties to a Custom Button...You also find that setBackgroudimage and setTitleColor properties here...
UIButtin *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setFrame:CGRectMake(165, 205, 65, 40)];
[myButton setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:.77] forState:UIControlStateNormal];
[myButton setTitle:#"Hi....." forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
[cell addSubview:myButton];
Thank u..
I guess what you want is to change your button background:
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
Your text disappears because you have set the image at button front side as button main image.
[cell.myButton setImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];
You need to set the button background image so that your text would be visible as the image will be drawn at the rear side of your UIButton context.
So just replace the above line of code to :
[cell.myButton setBackgroundImage:[UIImage imageNamed:#"mynewimage.png"] forState:UIControlStateNormal];

button image change using predefined method

In my app am using UIScrollView. I added buttons to UIScrollView.
For each button i assigned image using
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
and actions.
forState i used UIControlStateNormal in viewDidLoad.
And when we click on button image has to change.
For this i used same method but forState value changed to UIControlStateSelected in ViewDidLoad.
But button image is not changing.
any one can help or suggest
You've to use like this
In ViewDidLoad Set images for normal and selected state
-(void)ViewDidLaod
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateSelected];
To perform selection image change can be done below code
-(void)buttonclick
[myButton setSelected:YES];
or
[myButton setSelected:NO];
Use this line of code I think this will be helpful for you.
-(void)viewDidLoad
{
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[myButton setFrame:CGRectMake(0,3,70,32)];
[myButton setImage:[UIImage imageNamed:#"barbutton.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(compete) forControlEvents:UIControlEventTouchUpInside];
}
-(void)compete
{
[myButton setImage:[UIImage imageNamed:#"barbutton1.png"] forState:UIControlStateNormal];
}
use
[button setImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"button_highlighed.png"] forState:UIControlStateHighlighted];

how to change button image in tableview when that button clicked

i need to place a check box in UItableview.
For that i place a box with out check image is place on button.
my code is,
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame=CGRectMake(100, 10, 100, 100);
[customButton setImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:customButton];
Now i need to change button image when button clicked.
where can i done this.
and how can i impalement this.
can any one please help me.
Thank u in advance.
//add this line
[customButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//then
-(void) buttonClicked:(id) sender
{
UIButton * button = (UIButton*) sender;
[button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateNormal];
}
You can write this line of code
[customButton setImage:[UIImage imageNamed:#"unselected2.png"] forState:UIControlEventTouchDown];
before
[cell.contentView addSubview:customButton];
and you ca select different images for different events and states