触摸Swift 3

时间:2017-03-27 16:21:56

标签: ios swift uibutton

我阅读了一些文章,但没有找到解决我问题的解决方案。目前我在Swift 3中构建了一个iOS应用程序。我在我的视图中放置了一个名为registerButton的UIButton。如果有人触摸按钮,则会出现一个新的ViewController(newController)。除此之外,当用户触摸按钮并按住按钮时,应更改registerButton.textlabel.setColor()的颜色? newController的表示有效,但不是textLabel的更改颜色。我在注册功能中改变了registerButton的颜色。有人有想法吗?感谢

var registerButton: UIButton = {
    let button = UIButton(type: .system) 
    button.setTitle("Registration", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.addTarget(self, action: #selector(registration), for: .touchUpInside) // Action
    return button
}()

   func registration(){

    let color = UIColor(r: 255, g: 0, b: 102)// red
    registerButton.setTitleColor(color, for: .highlighted)
    let newController = ViewController()

    present(newController, animated: true, completion: nil)

  }

Button normal

Button pressed no red text label

2 个答案:

答案 0 :(得分:3)

var registerButton: UIButton = {
let button = UIButton(type: .system) 
button.setTitle("Registration", for: .normal)
button.setTitleColor(UIColor.white, for: .normal)
button.addTarget(self, action: #selector(colorChange), for: .touchDown)
button.setTitleColor(.red, for: .highlighted)
// Action
return button
}()

这应该可以正常使用

答案 1 :(得分:-2)

尝试在创建按钮时更改状态聚焦的颜色,而不是在按下时。

     var registerButton: UIButton = {
    let button = UIButton(type: .system) 
    button.setTitle("Registration", for: .normal)
    button.setTitleColor(UIColor.white, for: .normal)
    button.setTitleColor(UIColor.red, for: .focused)
    button.addTarget(self, action: #selector(registration), for: .touchUpInside) // Action
    return button
}()
相关问题