ios - Swift 中的 UIAppearance 和圆角边框

标签 ios swift exc-bad-access uiappearance

所以我对整个 UIAppearance 做事的方法相当陌生,而且做起来很快。为没有大量文档而欢呼吧。

我正在尝试通过 UIAppearance 设置我的边界半径,方法如下:

CircleButton.appearance.roundBorderRadius = 9

我的 CircleButton 类实现:

public class CircleButton : UIButton{
     @nonobjc var roundBorderRadius: CGFloat? {
        get { return self.layer.cornerRadius }
        set {
            self.layer.cornerRadius = newValue!
        }
    }
}

然后我将 Storyboard 中的所有内容都连接到包含 CircleButton 的 ViewController。没有编译或构建错误。

但是,在运行时我得到:

“线程 1:EXC_BAD_ACCESS”错误:

CircleButton.appearance.roundBorderRadius = 9

有什么建议吗?

最佳答案

删除@nonobjc并添加dynamic,然后将类型从CGFloat?更改为CGFloat并删除! 在 setter 中的 newValue 之后,像这样:

public class CircleButton: UIButton {

    dynamic var roundBorderRadius: CGFloat {
        get { 
            return layer.cornerRadius
        }

        set {
            layer.cornerRadius = newValue
        }
    }
}

关于ios - Swift 中的 UIAppearance 和圆角边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37550154/

相关文章:

ios - Swift 中的 Snapchat API

ios - 访问swift数组时swift_isUniquelyReferenced_nonNull_native中的EXC_BAD_ACCESS

iphone - 释放 UIColor PatternImage 的正确方法

ios - 将自定义类对象存储在核心数据中

ios - 在移动设备中使用正则表达式模式的 html5 输入类型编号

ios - 贴纸包扩展加载到 iPhone 7 的模拟器上,而不是 7+ 或 iPad pro?

ios - 在 Atlas.h 中找不到 ATLAddressBarViewController.h

objective-c - 函数声明中的预期标识符 - Objective-C 到 Swift

swift - UICollectionView 中的节标题,只出现第一个

ios - EXC_BAD_ACCESS 代码=2 地址=0x0