What's the right UIControlState for the UIButt

2019-04-07 17:25发布

问题:

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...

回答1:

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];


回答2:

Figure it out, it works in this way:

[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:@"ActivateButton.png"] forState:UIControlStateSelected];
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:@"ActivateButton.png"] forState:(UIControlStateHighlighted|UIControlStateSelected)];


回答3:

While we add a button from IDE

sample:

.h file
-(IBAction)BtnAdd:(id)sender

in .m file it is

-(IBAction)BtnAdd:(id)sender
{
}

It is a method that cannot enable or disable.

so if you want to enable or disable button make it as -(IBOutlet)BtnAdd add IBOutlet to .h file And connect it to the particular button then BtnAdd.enabled=NO will work