UIButton自定义类型,背景颜色和透明前景图像不起作用

时间:2013-05-23 18:28:09

标签: ios objective-c cocoa-touch uibutton

我有以下自定义按钮:

    _button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.backgroundColor = [UIColor darkGrayColor];

    UIImage *bg = [UIImage imageNamed:@"btn_bg_highlighted.png"];
    [_button setBackgroundImage:bg forState:UIControlStateHighlighted];

正如所料,该按钮具有深灰色背景。当我按下按钮时,会显示背景图像(红色方块)。

稍后在程序中我将按钮前景图像设置为透明(红色矩形)。之后,灰色背景变为白色。当我按下按钮时,它变成灰色。

当我在[UIButton buttonWithType:UIButtonTypeCustom]之后立即设置图像时效果很好。

enter image description here

你知道问题是什么吗?

1 个答案:

答案 0 :(得分:0)

好的,我很蠢。我从NSData对象获取图像。在服务器上,UIImageNSData转换为UIImageJPEGRepresentation(image, quality)。但这应该是UIImagePNGRepresentation(image),否则透明度会在客户端丢失。现在一切都很棒!