Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right UIControlState for the UIButton being pressed?

I'm customizing the UIButton programmatically here:

button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setSelected:YES];
        button.frame = CGRectMake(x, y, width, height);
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [button setTitle:@"Button Title" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateSelected];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];
        [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateDisabled];

The issue is if I'm holding down the image background image disappears until I'm releasing it...

like image 678
el.severo Avatar asked Nov 03 '11 18:11

el.severo


1 Answers

I think you're in overkill mode :). Try setting button.png for UIControlStateNormal and buttonActive.png for UIControlStateHighlighted. No need for the rest. See if this works.

EDIT:

Also, remember: Image file names are case sensitive

Are you testing on device? Image names are case sensitive for device builds, but not for the simulator. For example, if your actual image file is named buttonactive.png, but you're calling it as buttonActive.png from your code, it will show up on the simulator, but not on the device.

Please ensure that the case for both image names matches the name of the actual file.

EDIT #2:

Try this code

button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setSelected:YES];
    button.frame = CGRectMake(x, y, width, height);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    [button setTitle:@"Button Title" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted];
like image 67
Sid Avatar answered Sep 21 '22 23:09

Sid