【问题标题】:UIButton with low alpha background but fully opaque title?具有低 alpha 背景但标题完全不透明的 UIButton?
【发布时间】:2016-05-06 09:29:27
【问题描述】:

如何使UIButton 具有透明背景但标题不透明?我尝试了以下

button.alpha = .5;
button.titleLabel.alpha = 1;

这并没有达到预期的效果。其他建议?

【问题讨论】:

  • 子类化按钮并在内部实现?

标签: ios objective-c


【解决方案1】:

只需将背景设为 0.5 alpha

button.backgroundColor = [UIColor colorWithRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:0.5];

【讨论】:

  • 我可以不通过设置颜色来做到这一点吗?
  • 按钮上有背景图片吗?
  • 您可以读取现有的 backgroundColor 并通过保持除 alpha 之外的所有组件相同来创建新的背景颜色
【解决方案2】:

尝试使用按钮的不同部分。


例如,如果这是您想要 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];

不过,我不知道其他解决方案。希望这会有所帮助。

【讨论】:

    【解决方案3】:

    你可以使用这个: 迅捷button.backgroundColor = UIColor.white.withAlphaComponent(0.7)

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 2021-03-26
      • 1970-01-01
      • 2014-06-24
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多