Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change image set on Button

Tags:

ios

iphone

ipad

I want to change the button image on buttons click event. Here is what i am trying.

-(IBAction)editObjectImage:(id)sender
{
    if (editButtonState == NO)
    {   
        [editButton setImage:nil forState:UIControlStateNormal];
        [editButton setImage:[UIImage imageNamed:@"done2.png"] forState:UIControlStateNormal];
    }
    else
    {
        [editButton setImage:nil forState:UIControlStateNormal];
        [editButton setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
    }
}

But my Button image is not changing. What's wrong with code?

like image 567
Rahul Avatar asked Jun 02 '11 07:06

Rahul


People also ask

How do I turn a picture into a button?

The image buttons in the HTML document can be created by using the type attribute of an <input> element. Image buttons also perform the same function as submit buttons, but the only difference between them is that you can keep the image of your choice as a button.

How do I make an image a button in CSS?

The default button in HTML can be changed to an image using CSS. The required button is selected using the respective CSS selector. The background property can then be set to include a background image and change the image type as required. The border of the button can also be removed to show only the image itself.

How do I resize a button image in Swift?

Using storyboard, select the button, then in the size inspect click the dropdown on size just above Content inset. There is a list of sizes to select from, and this will adjust your image size(if you use system image). The default size is already set when you added the button on the storyboard to the View Controller.


2 Answers

I think you aren't getting round to changing editButtonState. Your code can be reduced to.

-(IBAction)editObjectImage:(id)sender
{
    UIButton *theButton = (UIButton*)sender;
    if (editButtonState == NO) {   
        [theButton setImage:[UIImage imageNamed:@"done2.png"] forState:UIControlStateNormal];
    } else {
        [theButton setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
    }

    editButtonState = !editButtonState;
}
like image 190
Deepak Danduprolu Avatar answered Oct 05 '22 02:10

Deepak Danduprolu


This is my working code:

NSString *shoppingListButtonImageName = @"notepad-selected";

UIImage *slImage = [UIImage imageNamed:shoppingListButtonImageName];
//put a breakpoint here to check that slImage is not nil.

[self.shoppingListButton setImage:slImage forState:UIControlStateNormal];
like image 38
Lay González Avatar answered Oct 05 '22 02:10

Lay González