我正在尝试在没有 Storyboard/界面生成器的情况下快速编程。当我按下 UIButton 时,它不会像我在 Storyboard文件中创建它那样改变颜色。如何启用此视觉功能?
这是(大致)我的代码:
import UIKit
class MyView: UIView {
var myButton: UIButton
init(buttonTitle: String) {
myButton = UIButton()
myButton.translateAutoresizingMaskIntoConstraints = false
myButton.setTitle(buttonTitle, forState: .Normal)
myButton.setTitleColor(.blueColor(), forState: .Normal)
super.init(frame: CGRect())
addSubview(myButton)
var layoutConstraints = [NSLayoutConstraints]()
//... autolayout ...
NSLayoutConstraint.activateConstraints(layoutConstraints)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
提前致谢。
最佳答案
请引用以下链接,它来自 objective-c 。你可以从这里找到一些概念, How to change the background color of a UIButton while it's highlighted?
他们已经回答了。明白了 friend !
关于ios - 如何使以编程方式生成的 UIButton 在按下时执行默认动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427207/