2012-06-02 27 views
0

我有一个设置放置故事板的UIButton的imageView属性的方法。它设置后,它看起来很好。但是,轻触按钮时,其高亮状态会将imageView的图像属性更改回旧图像。我如何阻止这种情况发生?这里是我的方法:如何在按下时阻止UIButton的imageView更改?

- (void)setThumbButtonPhoto:(UIImage *)image 
{ 
    // profilePhoto is an IBOutlet property of class UIButton pointing to the 
    // UIButton on my storyboard. 

    // Button image is changed correctly here 
    profilePhoto.imageView.image = image; 

    // But then mysteriously changed back to the old image when tapped. 
    // The following commented out lines I have all tried (one at a time of course) 
    // and none have solved my problem --> 

    // [profilePhoto.imageView setHighlightedImage:nil]; 
    // profilePhoto.imageView.highlightedImage = image; 
    // profilePhoto.adjustsImageWhenHighlighted = NO; 
    // [profilePhoto setBackgroundImage:image forState:UIControlStateHighlighted]; 
    // [profilePhoto setImage:image forState:UIControlStateNormal]; 
    // [profilePhoto setImage:image forState:UIControlStateHighlighted]; 
    // [profilePhoto setImage:image forState:UIControlStateSelected]; 
} 
+0

取消注释所有这三个线 // [profilePhoto setImage的:图像forState :UIControlStateNormal]; // [profilePhoto setImage:image forState:UIControlStateHighlighted]; // [profilePhoto setImage:image forState:UIControlStateSelected]; 在一起运行,并告诉我发生了什么 –

+0

它改变了按钮的背景图像,而不是像我想要的内部imageView图像。 – zakdances

回答

0

使用的UIImageView保存图像

,把一个自定义模式的UIButton,没有文字,没有图片,没有背景;在它之下

2

您应该使用此方法来正确设置图片上的按钮: - (void)setImage:(UIImage *)image forState:(UIControlState)state

所以:

[profilePhoto setImage:image forState:UIControlStateNormal]; 
[profilePhoto setImage:image forState:UIControlStateHighlighted]; 
+0

这些行会更改按钮的整个背景图像,而不是按钮内的图像。 imageView属性指向按钮的图像INSIDE(有点像图标)。当我在profilePhoto上使用setImage时,它会更改整个按钮的背景图像。 – zakdances

相关问题