xcode 按下按钮时更改 UIButton 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12864571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:48:30  来源:igfitidea点击:

Change UIButton image when button pressed

iphoneobjective-cxcode

提问by Jo?o Paulo

I'm trying to change the image button just in the moment that the button is pressed. When released, i want go back to the original.

我正在尝试在按下按钮的那一刻更改图像按钮。发布后,我想回到原来的状态。

I read some posts here, but it's not working.

我在这里阅读了一些帖子,但它不起作用。

Is it possible do it in the GUI without programming?

是否可以在没有编程的情况下在 GUI 中完成?

I tried this:

我试过这个:

-(IBAction) toggleUIButtonImage:(id)sender{
     if ([sender isSelected]) {
         [sender setImage:unselectedImage forState:UIControlStateNormal];
         [sender setSelected:NO];
     }else {
         [sender setImage:selectedImage forState:UIControlStateSelected];
         [sender setSelected:YES];
      }
 } 

But what kind of sender events should i associated the function toggleUIButtonImage ?

但是我应该将什么样的发送者事件关联到功能 toggleUIButtonImage ?

Thanks

谢谢

回答by PostPCDev

You can do it in interface builder by giving different settings to each button state config:

您可以在界面构建器中通过为每个按钮状态配置提供不同的设置来实现:

The available states are: default, selected and disabled

可用状态有:默认、选中和禁用

Or you can also change the 'settings' of the button in code by using 'UIButton' setImage:forState method.

或者,您也可以使用“UIButton” setImage:forState 方法在代码中更改按钮的“设置”。

    [myButton setImage:highlightedImage forState:UIControlStateHighlighted];

Usually, you should do it by 'configuring' the button as described above and not by changing the sub views of the button manually as a response to user interaction. The reason it doesn't work for you is the inherent hidden behaviour of UIButton. You manually make the change of a subview of the button as a response to user interaction, but your change is changed back to what was defined in the settings of your button (according to the button's state). So it seems like your code doesn't do anything at all.

通常,您应该通过如上所述“配置”按钮来实现,而不是通过手动更改按钮的子视图来响应用户交互。它对您不起作用的原因是 UIButton 固有的隐藏行为。您手动更改按钮的子视图以响应用户交互,但您的更改将更改回按钮设置中定义的内容(根据按钮的状态)。所以看起来你的代码根本没有做任何事情。

回答by Mutawe

in the ViewDidLoadMethod

ViewDidLoad方法

set the property of

设置属性

[[button setImage:[UIImage ImageNamed:@"unselectedImage.png"] forState: UIControlStateNormal]; 
[[button setImage:[UIImage ImageNamed:@"selectedImage.png"] forState: UIControlStateHighlighted ];

回答by Scott

This can be done with Interface Builder, no code necessary.

这可以通过 Interface Builder 完成,不需要代码。

Select your button in IB, go to the right sidebar, change the Type to Custom, make sure State Config is Default, and type in the name of your "regular image" reference in the Background Image text field.

在 IB 中选择您的按钮,转到右侧边栏,将类型更改为自定义,确保状态配置为默认,然后在背景图像文本字段中输入“常规图像”引用的名称。

Then change State Config to Highlighted, and type in the name of your "pressed image" reference in the Background Image text field.

然后将状态配置更改为突出显示,并在背景图像文本字段中输入“按下图像”引用的名称。

回答by Greg

In IB, you can set the image for the Highlighted (not Selected) state of the button. That should do it.

在 IB 中,您可以为按钮的突出显示(未选中)状态设置图像。那应该这样做。

Look in the Button section of the Utilities pane, and select Highlighted from the State Config drop-down.

查看“实用程序”窗格的“按钮”部分,然后从“状态配置”下拉列表中选择“突出显示”。