在UIButton中显示图像 [英] Displaying image in UIButton

查看:98
本文介绍了在UIButton中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要开始这个问题,我对Objective-C和Xcode还是陌生的.我现在只学习了一个星期左右,但是我在这个问题上停留了几天.我正在尝试在UIButton中显示图像.我有3种可供选择,blue.png,purple.png和orange.png.无论我做什么或告诉Xcode使用什么图像,它都只会显示蓝色图像.我已经从项目中完全删除了图像.我在iOS模拟器中有重置内容和设置".到底是怎么回事?

To preface this question, I am extremely new to Objective-C and Xcode. I have only been learning for a week or so now but I have been stuck on this problem for a few days. I am trying to display an image in a UIButton. I have 3 to choose from, blue.png, purple.png and orange.png. No matter what I do or what image I tell Xcode to use, it will only display the blue image. I have deleted the image from the project entirely. I have "reset content and settings" in the iOS simulator. What exactly is going on here?

.h代码

IBOutlet UIButton *h1;

.m代码

UIImage *buttonImage = [UIImage imageNamed:@"purple.png"];
[h1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:h1];

无论我在引号中指定哪个图像,它都始终使用blue.png,即使该图像不再存在于项目中也是如此.谢谢大家的帮助!

No matter which image I specify within the quotes, it always uses blue.png, even when the image no longer exists in the project. Thanks for the help everybody!

推荐答案

您必须已将blue.png设置为情节提要或xib文件中的背景图像.编写NSLog(@%@",buttonImage)以确保图像不是nil.确保输入正确的名称和扩展名purple.png

You must have set blue.png as the background image in the storyboard or xib file. Write NSLog(@"%@",buttonImage) to make sure that the image is not nil. Make sure that you are entering the correct name and extension purple.png

而且,使用

[h1 setBackgroundImage:buttonImage forState:UIControlStateNormal];

而不是setImage

这篇关于在UIButton中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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