【发布时间】:2016-05-06 09:29:27
【问题描述】:
如何使UIButton 具有透明背景但标题不透明?我尝试了以下
button.alpha = .5;
button.titleLabel.alpha = 1;
这并没有达到预期的效果。其他建议?
【问题讨论】:
-
子类化按钮并在内部实现?
标签: ios objective-c
如何使UIButton 具有透明背景但标题不透明?我尝试了以下
button.alpha = .5;
button.titleLabel.alpha = 1;
这并没有达到预期的效果。其他建议?
【问题讨论】:
标签: ios objective-c
只需将背景设为 0.5 alpha
button.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:0.5];
【讨论】:
尝试使用按钮的不同部分。
例如,如果这是您想要 alpha 0.5 的图像,那么试试这个:
[button setImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
button.imageView.alpha = 0.5;
甚至这个:
button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"myImage"]];
button.backgroundColor = [button.backgroundColor colorWithAlphaComponent:0.5];
你甚至可以直接在 UIImage 上添加 alpha -- How to set the opacity/alpha of a UIImage?
如果您通过以下方式设置图像,上述链接会有所帮助:
[button setBackgroundImage:[UIImage imageNamed:@"myImage"] forState:UIControlStateNormal];
不过,我不知道其他解决方案。希望这会有所帮助。
【讨论】:
你可以使用这个:
迅捷button.backgroundColor = UIColor.white.withAlphaComponent(0.7)
【讨论】: