如何在iOS按钮上更改背景图像? [英] How to change background image on a button , iOS?

查看:144
本文介绍了如何在iOS按钮上更改背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图控制器上创建了一个具有预定义背景图像的按钮。我为这个按钮创建了一个动作和一个插座。我希望当用户点击按钮更改此按钮的背景图像时。我怎样才能做到这一点?
我试图把按钮的动作方法放到这样的东西:

I created a button on my view controller which has a predefined background image. I created an action and an outlet for this button. I want when the user taps the button to change the background image of this button. How can i do that? I tried to put into the action method of the button something like this:

snapshotCheckbox.image = [UIImage imageNamed:@"snapshot.png"];

但我想这个方法适用于UImageViews。我怎样才能为按钮做同样的事情?
非常感谢您阅读我的帖子:D

but i guess this method is for UImageViews. How can i do the same thing for a button? Thank you very much for reading my post :D

推荐答案

执行此操作的一个解决方案是显示图像在 UIImageView 中,并在UIImageView上放置一个透明的UIButton。在Interface Builder中,您可以将UIButton更改为custom。
这样可以在处理按下UIButton时触发的动作时轻松更改UIImageView中显示的图像。

One solution to do this would be to display the image in a UIImageView and put a transparent UIButton on top of the UIImageView. In Interface Builder you can change a UIButton to "custom". This would allow you to change the image displayed in the UIImageView easily when handling the action triggered when the UIButton is pushed.

希望这会有所帮助。

这篇关于如何在iOS按钮上更改背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆