【问题标题】:UIButton Custom Type with background color and transparent foreground image not working具有背景颜色和透明前景图像的 UIButton 自定义类型不起作用
【发布时间】:2023-03-14 15:44:01
【问题描述】:

我有以下自定义按钮:

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

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

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

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

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

你知道问题出在哪里吗?

【问题讨论】:

标签: ios objective-c cocoa-touch uibutton


【解决方案1】:

好吧,我很愚蠢。我从NSData 对象获取图像。在服务器上,UIImage 被转换为 NSDataUIImageJPEGRepresentation(image, quality)。但这应该是UIImagePNGRepresentation(image),否则客户端会丢失透明度。现在一切正常!

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多