2012-07-10 61 views
2

我想在viewWillAppear上设置按钮的图像。无法更改UIButton的图像

我已经尝试了以下所有代码 -

UIButton *front; 
    UIImage *temp= [UIImage imageNamed:@"06_asystole.jpg"]; 
     [front setBackgroundImage:temp forState:UIControlStateNormal]; 

     //front.imageView.image = [UIImage imageNamed:@"06_asystole.jpg"]; 
     /// [front setBackgroundImage:[UIImage imageNamed:@"06_asystole.jpg" forState:UIControlStateNormal]]; 
     // [front setImage:[UIImage imageNamed:@"06_asystole.jpg" forState:UIControlStateNormal]]; 

我还设置IBOutlet中对按钮,并在厦门国际银行进行分配。

我不知道为什么按钮的图像不能设置。

谢谢..

+0

是正面的图像或按钮的名称? – 2012-07-10 07:34:24

+0

你能发表更多的代码吗 – 2012-07-10 07:37:43

+0

如果你做'NSLog(@“%@”,temp)会发生什么;'? – basvk 2012-07-10 09:36:19

回答

3

不会工作:

UIButton *front = [[UIButton alloc]init]; 
UIImage *temp= [UIImage imageNamed:@"06_asystole.jpg"]; 
[front setBackgroundImage:temp forState:UIControlStateNormal]; 

你想改变现有的UIButtonbackgroundImage。要么现在将front添加到您的视图中,并删除旧视图,或者使用现有的按钮名称而不是front

+0

感谢您的回复。我也试过了。但它不起作用 – 2012-07-10 07:35:59

+0

'NSLog(@“%@”,button);'并发布输出。 //“06_asystole.jpg”是你的捆绑包的一部分吗? – 2012-07-10 07:37:04

+0

NSLog是 - > – 2012-07-10 07:40:14

0

试试这样说:

UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom]; 
btn.frame = CGRectMake(40, 140, 240, 30); //this should be something valid for you... 
UIImage *temp= [UIImage imageNamed:@"06_asystole.jpg"]; 
     [btn setBackgroundImage:temp forState:UIControlStateNormal]; 

框架应是图像或东西对你有用的大小。

如果您正确链接了xib中的按钮,您应该可以直接从编辑器编辑按钮。

0

请试试这个,

UIButton *front = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIImage *img = [UIImage imageNamed:@"06_asystole.jpg"]; 
[front setImage:img forState:UIControlStateNormal]; 

希望这会工作。

如果你已经添加了一个按钮到你的xib,那么不需要创建一个新的按钮。 您可以通过代码或通过厦门国际银行设置的图像到该按钮像如下,

Button image via xib 在厦门国际银行选择按钮,设置属性检查器上的图像性能。

检查此链接的UIButton类参考。 link

2
[btn setImage:[UIImage imageNamed:@"06_asystole.jpg"] forState:UIControlStateNormal]; 
0

我已经通过在我的应用程序中重新添加图像解决了我的问题。虽然我已经添加了图像捆绑,它没有得到该图像的参考。所以我删除了旧图像并重新添加了它。比它工作正常。

谢谢大家的帮助。 :)

0

试试这个

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setBackgroundImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateHighlighted];