ios - 禁用和启用状态的 UIButton tintColor?

标签 ios swift uibutton

我有一个继承自 UIButton 的自定义 class。我想要完成的事情是根据按钮的启用状态(即启用或禁用)设置 tintColor 属性。

有什么办法可以实现吗?

这是我的课:

class ButtonsPostMenu: UIButton
{
    override func awakeFromNib()
    {
        titleLabel?.font = UIFont(name: Font_AvenirNext_Medium, size: 14)
        tintColor = UIColor.white
    }
}

最佳答案

您可以覆盖 isEnabled 属性来实现这一点。 tintColor 将根据按钮的 isEnabled 状态自动更改:

class ButtonsPostMenu:UIButton {

    //......

    override var isEnabled: Bool {
        didSet{
            if self.isEnabled {
                self.tintColor = UIColor.white
            }
            else{
                self.tintColor = UIColor.gray
            }
        }
    }

    //......

}

关于ios - 禁用和启用状态的 UIButton tintColor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45834966/

相关文章:

ios - 同一父 View 中的两个容器 View 使用相同的数据

ios - 'SwiftClass' 不可用 : cannot find Swift declaration for this class - iOS Simulator only

ios - 如何让Uibutton被选中

objective-c - 为 UIButton 添加背景阴影

ios - 从另一个 View Controller 快速修改一个变量

ios - 表格 View 显示在屏幕下方

ios - 这个 Xcode 项目文件夹层次结构最可能的设计模式是什么?

ios - 在 Swift 中没有 FBSDKLoginButton 的 Facebook 登录

ios - 在每 x 次加载 viewdidload 时显示插页式广告

iphone - UIButton 图像不会在 "for"循环中更改