【问题标题】:UIButton title won't highlight when an image / background image is set设置图像/背景图像时,UIButton 标题不会突出显示
【发布时间】:2018-01-10 09:44:37
【问题描述】:

我有一个带有右对齐箭头图标的自定义按钮。

当它通过点击突出显示时,仅突出显示箭头图标。文本保持白色,而不是像图标那样变成灰色。

这是代码(在 UIButton 的子类中):

let rightIcon = #imageLiteral(resourceName: "disclosureIndicator")

setTitleColor(.white, for: .normal)
setBackgroundImage(rightIcon, for: .normal)

guard let image = backgroundImage(for: .normal) else { return }

titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: image.size.width)

我还覆盖了 backgroundRect 属性。

override func backgroundRect(forBounds bounds: CGRect) -> CGRect {
    guard let image = backgroundImage(for: .normal) else {
        return super.backgroundRect(forBounds: bounds)
    }
    return CGRect(
        x: frame.width - (image.size.width + 20),
        y: (frame.height / 4) + (image.size.height / 4),
        width: image.size.width,
        height: image.size.height)
}

我也尝试过设置按钮图像(即setImage(rightIcon, .normal))而不是设置按钮背景图像,但没有成功。

我也尝试将图标的突出显示颜色和标题设置为灰色,但这也不起作用。

我希望在点击按钮时突出显示文本和图标,但我似乎找不到实现此目的的方法。真的做不到吗?

【问题讨论】:

标签: ios image uibutton icons title


【解决方案1】:

将按钮类型设置为System 而不是Custom 解决了这个问题。现在他们都像他们应该的那样突出显示。

【讨论】:

  • 按此按钮后图像变为蓝色。
猜你喜欢
  • 2012-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-11
相关资源
最近更新 更多