如何为 UIControlStateSelected 设置按钮图像
您好,我想在按下按钮后更改按钮的背景,即按钮不应变回之前的图像。
但是,我无法这样做。我的代码如下。谁能告诉我如何在单击按钮后将图像更改为“custom_button_highlight”?
UIImage *buttonImageNormal = [UIImage imageNamed:@"custom_button"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:@"custom_button_highlight"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateHighlighted];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateSelected];
Hi I want to change the background of my button after I press the button i.e. the button should not change back to the previous image.
However, I am not able to do so. My code as follows. Can anyone advise how I can change the image to "custom_button_highlight" after I click on the button?
UIImage *buttonImageNormal = [UIImage imageNamed:@"custom_button"];
UIImage *stretchableButtonImageNormal = [buttonImageNormal
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
UIImage *buttonImagePressed = [UIImage imageNamed:@"custom_button_highlight"];
UIImage *stretchableButtonImagePressed = [buttonImagePressed
stretchableImageWithLeftCapWidth:12 topCapHeight:0];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[button setBackgroundImage:stretchableButtonImageNormal
forState:UIControlStateNormal];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateHighlighted];
[button setBackgroundImage:stretchableButtonImagePressed
forState:UIControlStateSelected];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
METHOD1
我发现您正在使用代码来生成
UIButton
。使用界面生成器更容易。试试这个 - 在界面构建器(Xcode4)中,创建按钮和打开右侧边栏。之后选择按钮,&按钮类型为自定义
。之后在state config
中,有多种状态,对于每种状态,您可以在image
中设置图像。希望这会有所帮助...METHOD2
如果您想使用代码生成
UIButton
(这让我不明白为什么)那么你似乎做了正确的事情。您确定图片在您的包中吗?您没有包含图像扩展名(如 .jpg 等)METHOD1
I see that you are using code to generate
UIButton
. It's easier to use Interface builder. Try this - In Interface builder (Xcode4), create the button & open the right sidebar. After that select the button, & the type of button ascustom
. After that instate config
, there are various states, for each state you can set an image inimage
. Hope this helps...METHOD2
If you want to use code to generate
UIButton
(it beats me why) then you seem to be doing the right thing. Are you sure the images are there in your bundle? You did not include the image extension (like .jpg etc.)你提到过,你不希望图像变回来......
那么您不必为突出显示或选择的状态设置它,而是必须为 UIControlStateNormal 状态设置它。
但是由于您在按下按钮后需要它,因此您必须捕获按钮被按下的事件并在其中设置背景图像。
希望它有帮助;)
You mentioned, that you don't want the image to change back...
then instead of setting it for the states highlighted or selected, you have to set it for the state UIControlStateNormal.
But since you need it after it's been pressed, you have to catch the event that your button has been pressed and set the background image in there.
Hope it helped ;)
...以编程方式执行您想要的操作,而不是背景图像。但我认为你应该遵循 Srikar 的答案并使用界面生成器(如果可以的话)。
... does what you want programatically, not the background image. But I think you should follow Srikar's answer and use interface builder if you can.