【问题标题】:How to remove grey layer on half transparent button when clicked单击时如何删除半透明按钮上的灰色层
【发布时间】:2016-07-03 17:19:54
【问题描述】:

我想禁用按下按钮时出现的灰色层。我的按钮有一个特殊的形状,其中一部分是透明的。

未按下:

按下:

我的按钮在 UIView 内。我的 UIView 有一个 clearColor() 背景。我已经尝试为 Normal 和 Highlighted 状态的按钮设置背景图片:

agendaButton.setBackgroundImage(UIImage(), forState: .Normal) agendaButton.setBackgroundImage(UIImage(), forState: .Highlighted)

我还尝试为两种状态设置相同的图像:

agendaButton.setImage(UIImage(named: "agenda"), forState: .Normal) agendaButton.setImage(UIImage(named: "agenda"), forState: .Highlighted)

我尝试将图像设置为我的视图的子视图并在顶部放置一个透明按钮:

agendaView.addSubview(UIImageView(image: UIImage(named: "agenda")))

我也尝试设置agendaButton.adjustsImageWhenHighlighted = false 并仅设置.Normale 状态的图像。或者为两种状态设置图像。我还有灰色层。我想知道是不是 UIView 变灰了,但显然不是。我将UIView背景设置为红色,并将按钮变小,我可以看到变灰的显然是按钮而不是UIView。

我尝试以编程方式完成所有操作,并将我的UIButton 类型设置为.CustomagendaButton.adjustsImageWhenHighlighted = falseagendaButton.setImage,然后使用agendaButton.setBackgroundImage,两者都用于.Normal.Highlighted 状态。相同的行为。

我想知道是否有办法停用这个灰色层。

问题是按钮的白色部分没有变成灰色,只有透明部分。我尝试了在 SO 上找到的多种解决方案,但找不到任何部分透明按钮。

【问题讨论】:

  • 尝试将按钮创建为自定义类型 (UIButton(type: .Custom))
  • @DantePuglisi 如果我检查情节提要中按钮的类型,它已经是Custom 类型。我应该尝试以编程方式 100% 添加按钮吗?
  • 我更像是一个“100% 以编程方式”的人,虽然我没有足够的 IB 经验,所以我不知道哪个更好
  • 我还尝试使用agendaButton.adjustsImageWhenHighlighted = falseagendaButton.setImage 以编程方式完成所有操作,然后使用agendaButton.setBackgroundImage 处理.Normal.Highlighted 状态。相同的行为:(
  • 您介意用代码更新您的问题吗?

标签: ios swift button uibutton


【解决方案1】:

在 IB 中,您可以取消选中 “突出显示调整图像”

您可以通过编程方式使用

UIButton.adjustsImageWhenHighlighted = NO;

“禁用”状态也有类似的选项。

【讨论】:

  • 对不起,我也试过了,但它不起作用,我会编辑我的问题。
【解决方案2】:

我发现了问题。我想您的所有解决方案都应该对我有所帮助。 我没有看到在代码的底部有这两行: let color = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 0.3)self.agendaButton.setBackgroundImage(self.generateImageWithColor(color), forState: .Highlighted) 我觉得我让你失去了你的时间。非常感谢您的帮助。

【讨论】:

  • 这就是为什么你应该在问题中发布你的代码
  • 是的,但它认为它会太长。但是下次我提出问题时,我会记得添加更多代码。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多