简体   繁体   English

轻按切换按钮的图像

[英]switch button's image on tap

I have the following code 我有以下代码

- (IBAction)switchSetting:(UIButton *)sender {
    if([sender.currentImage isEqual:[UIImage imageNamed:@"settings_on"]]){
        [sender setImage:nil forState:UIControlStateNormal];
        //[sender setImage:[UIImage imageNamed:@"settings_off"] forState:UIControlStateHighlighted];
        [sender setTintColor:[UIColor clearColor]];
    }else{
        [sender setImage:[UIImage imageNamed:@"settings_on"] forState:UIControlStateNormal];
        //[sender setImage:[UIImage imageNamed:@"settings_on"] forState:UIControlStateSelected];
        //[sender setImage:[UIImage imageNamed:@"settings_on"] forState:UIControlStateReserved];
        //[sender setImage:[UIImage imageNamed:@"settings_on"] forState:UIControlStateHighlighted];
        [sender setTintColor:[UIColor clearColor]];
    }
}

All buttons background images are set to "settings_off". 所有按钮背景图像均设置为“ settings_off”。 That way when I want to switch the settings I either set the buttons image to "settings_on" or NIL. 这样,当我想切换设置时,可以将按钮图像设置为“ settings_on”或NIL。

Problem is when I run the app, the buttons that has an image set to "settings_on" work fine (well, one problem is I need to click twice the first time to make the switch, but thats minor for now) but the buttons without an image set never switch. 问题是当我运行该应用程序时,将图像设置为“ settings_on”的按钮可以正常工作(嗯,一个问题是,我第一次需要单击两次以进行切换,但那是次要的),但是没有图像集永远不会切换。 I have tried settings the images for the various states (selected, highlighted, normal), when I do this, then after the switch from on to off(nil), it won't switch back (as opposed to when being commented out). 我尝试设置图像的各种状态(选中,突出显示,正常),当我这样做时,然后从打开切换到关闭(无)后,它就不会切换回(与被注释掉相对) 。 Im lost, any help appreciated thanks! 我迷路了,任何帮助表示感谢!

EDIT: Clearing all buttons from their backgroundImages, and only using "image" got it working; 编辑:从其backgroundImages清除所有按钮,并且仅使用“图像”使其工作; even though I still don't understand why my initial approach shouldn't be working. 即使我仍然不明白为什么我最初的方法不起作用。 AFAIK the image view will overlay the background view... Does anybody know why I have to click twice the first time to get the switch going? AFAIK图像视图将覆盖背景视图...有人知道为什么我必须第一次单击两次才能使开关启动吗?

Have you tried the below code? 您是否尝试过以下代码?

- (IBAction)switchSetting:(UIButton *)sender {
if([sender backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"settings_off"] ){
    [sender setImage:[UIImage imageNamed:@"settings_on"] forState:UIControlStateNormal];
}else{
    [sender setImage:[UIImage imageNamed:@"settings_off"] forState:UIControlStateNormal];
}

You can check UIButton Image with sender.currentImage : 您可以使用sender.currentImage检查UIButton图像:

- (IBAction)switchSetting:(UIButton *)sender {

if ([sender.currentImage isEqual:[UIImage imageNamed:@"settings_off"]]) 
{
[sender setImage:[UIImage imageNamed:@"settings_on"] forState:UIControlStateNormal];

}
else
{
[sender setImage:[UIImage imageNamed:@"settings_off"] forState:UIControlStateNormal];


}

}

Hope this will help you in checking button current image . 希望这可以帮助您检查按钮当前图像。

您可以尝试相应地在按钮上设置tag值,然后再使用switch语句检查条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM