What is the correct UIControlState to push UIButton?

I configure 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 problem is that if I hold the image, the image disappears until I release it ...

+6
source share
3 answers

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

EDIT:

Also, remember: Image file names are case sensitive.

Are you testing a device? Image names are case sensitive for device assembly, but not for the simulator. For example, if your actual image file is called buttonactive.png, but you call it as buttonActive.png from your code, it will be displayed on the simulator, but not on the device.

Make sure 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]; 
+8
source

Change this, it works as follows:

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

While we add a button from the IDE

Example:

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

in .m file

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

This is a method that cannot enable or disable.

therefore, if you want to enable or disable the button, make it as -(IBOutlet)BtnAdd add the IBOutlet file to .h and connect it to a specific button then BtnAdd.enabled=NO will work

-2
source

Source: https://habr.com/ru/post/900726/


All Articles