steven
steven

Reputation: 698

Buttons changing background images on iPhone

I have it working with a small error. I have two code pieces for each button. One being this:

-(IBAction)clickoneButton:(id)sender
{
    [sender setBackgroundImage:[UIImage imageNamed:@"cnumber_1.jpg"] forState:UIControlStateNormal];
}

And the other being this:

-(IBAction)oneButton:(id)sender
{
    self.enteredPhoneNumberString = [self.enteredPhoneNumberString stringByAppendingString:[NSString stringWithFormat:@"%d", 1]];
    [self updateFormattedPhoneNumberLabel];

    [sender setBackgroundImage:[UIImage imageNamed:@"Number_1.jpg"] forState:UIControlStateNormal];
}

The first is set up to be Touch Down. The other is for Touch Up Inside. The app looks like the phones regular dialer. So the first image is a light blue button and the second is a dark blue button just the same way as the iPhones dialer is. The issue is when I press down on the button it almost seems like the dark image is still behind the Touch Down light blue image. It is changing to a lighter colored image but is shaded almost like the dark image is transparent through it or something. Not sure how else to explain it. If you have any ideas as to why the light blue is darkened some when the Touch Down is happening I would appreciate any help. Thanks

Button before touch and after release Button before touch and after release (Correct)

Button on Touch Down Button on Touch Down (not working properly)

What the Touch Down button should look like What the touch down button should look like

Upvotes: 0

Views: 1950

Answers (1)

David M. Syzdek
David M. Syzdek

Reputation: 15788

Based upon the two snippets of code you provided, you are not setting an image for when the button is pressed. Try updating your code to use the following when you load the view:

// sets the background image for when the button is not pressed
[sender setBackgroundImage:[UIImage imageNamed:@"cnumber_1.jpg"] forState:UIControlStateNormal];

// sets the background image for when the button is pressed
[sender setBackgroundImage:[UIImage imageNamed:@"Number_1.jpg"] forState:UIControlStateHighlighted];

Upvotes: 3

Related Questions