ios - 如何使以编程方式生成的 UIButton 在按下时执行默认动画?

标签 ios swift uibutton

我正在尝试在没有 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/

相关文章:

xcode - Swift + Xcode 6 beta 3 + Core Data = awakeFromInsert 没有被调用?

ios - 您可以使用 CoreData 中已有的数据上传 iPhone 审查应用程序吗?

swift - 在选项卡菜单中推送 View 时选项卡栏消失?

swift - 如何将之字形边框添加到圆形 ImageView

ios - 在 swift 中使用 userdefaults 保存复选框按钮状态

ios - 使用 UILabel 作为 UIButton 的 subview 自动调整文本大小 - swift

ios - Xcode - 单元测试 - 为 iOS 12 编译,但模块的最小部署目标为 13

ios - 在 Storyboard 中的 View 之间切换

ios - 电池被过度重复使用

ios - 将触摸从 UIButton 的 subview 传递回 UIButton