Search code examples
ios6xcode4uibutton

When I change the background of my UIButton it resizes


I am looking to change my UIButton background image using a click event but when I do the buttons resize, what can I do to prevent this? The code I'm using is below.

UIImage *btnImage = [UIImage imageNamed:@"Bttn_DateSel_Up.png"];
[startDtSelBttn setImage:btnImage forState:UIControlStateNormal];
UIImage *btnImage2 = [UIImage imageNamed:@"Bttn_DateSel_Down_Clear.png"];
[endDtSelBttn setImage:btnImage2 forState:UIControlStateNormal];

Solution

  • Something like this would help you:

    -(IBAction)btnClicked:(id)sender
    {
      [startDtSelBttn setImage:SomeOtherButtonImageNameHere forState:UIControlStateNormal]
    }
    

    This will change the image of the button startDtSelBttn.

    Also, if you want your button to have two images one for normal state and one for selected image, then you can write your code like this:

    UIImage *btnImageNormal   = [UIImage imageNamed:@"Bttn_DateSel_Up.png"];
    UIImage *btnImageSelected = [UIImage imageNamed:@"Bttn_DateSel_Up_Selected.png"];
    [startDtSelBttn setImage:btnImageNormal forState:UIControlStateNormal];
    [startDtSelBttn setImage:btnImageSelected forState:UIControlStateSelected];
    

    Hope it will help you.