ios - 禁用时更改 UIButton 边框

标签 ios objective-c

我想在禁用时隐藏按钮的边框。启用后,我这样设置边框:

_btnInit.layer.borderWidth = 1;
_btnInit.layer.borderColor = [[UIColor grayColor]CGColor];

当它被禁用时,我想删除边框。 enter image description here

如图所示。左侧为禁用状态,右侧为正常状态。

最佳答案

子类化 UIButton 并像这样覆盖 isEnabled 属性,

override var isEnabled: Bool {
    didSet {
        if isEnabled {
            self.layer.borderColor = UIColor.black.cgColor
            self.layer.borderWidth = 1.0
        } else {
            self.layer.borderWidth = 0.0
        }
    }
}

关于ios - 禁用时更改 UIButton 边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49045324/

相关文章:

ios - 如何在 SwiftUI 中旋转文本、按钮、矩形?

ios - 具有现有仿射变换的 UIKit Dynamics

android - 最近更新后 react-native run-ios 失败

ios - 如何将图像设置为在另一个viewController中异步获取的UIImageView?

ios - swift 中的 filterUsingPredicate 给出错误 "Void is not convertible to NSArray"

ios - 使用 Swift 在 iOS 上将身份验证凭据添加到钥匙串(keychain)

ios - 检测 USB 是否连接到 iPhone 设备

iphone - 尝试删除 TableView 部分时的 EXC_BAD_ACCESS

ios - 我如何知道哪个场景称为特定场景?

ios - touchesBegan 方法的问题