Swift - 将背景颜色更改为透明

KTO*_*TOV 5 swift

我注意到我的一个按钮的背景颜色值为 'default'

当按钮突出显示时,我想更改一个不同的按钮以具有“默认”背景颜色,这是迄今为止我的代码:

@IBAction func buttonTouchUpInside(_ sender: UIButton) {

    registerBtn.setTitleColor(UIColor.white, for: UIControlState.highlighted);
}
Run Code Online (Sandbox Code Playgroud)

我如何实现这一点,以便 registerBtn 具有基本上透明的背景颜色?

dfd*_*dfd 7

在评论中我发现:

  • 这两个按钮都是在 InterfaceBuilder (IB) 中创建的。
  • 每个按钮的背景颜色要么设置为默认值,要么在 IB 中设置为其他颜色。
  • 这个想法是让第二个按钮在第一个突出显示时具有透明的背景颜色和白色标题。

(我将根据需要编辑上面的内容。我不完全确定的是当第一个按钮不再突出显示后会发生什么。)

如果是上述情况,则需要进行以下操作:

  • 当UIControlEventTouchDown 或 UIControlEventTouchDragEnter 发生时让第一个按钮执行操作。
  • 拥有第一个按钮,然后通过代码更改第二个按钮的背景颜色。

可能最简单的方法不是通过 IB,而是通过代码。在 IB 中添加按钮后,为它们创建 IBOutlets。然后你可以这样做:

@IBOutlet weak var firstButton: UIButton!
@IBOutlet weak var secondButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    firstButton.addTarget(self, action: #selector(highlightSecondButton), for: .touchDown)
    firstButton.addTarget(self, action: #selector(highlightSecondButton), for: .touchDragEnter)
}

func highlightSecondButton(_ sender:UIButton) {
    secondButton.setTitleColor(UIColor.white, for: UIControlState.normal)
    secondButton.backgroundColor = nil
}
Run Code Online (Sandbox Code Playgroud)


eye*_*e17 6

用于UIColor.clear使颜色透明。

苹果称其为“默认”而不是“清晰”,这非常令人困惑。