ios - 更改 Storyboard 中的 UIButton BorderColor

标签 ios colors uibutton storyboard

我在用户定义的运行时属性中为 UIbutton 设置了 CornerRadius 和 BorderWidth。在不添加 layer.borderColor 的情况下,它可以正常工作并以黑色显示边框。但是当添加 layer.borderColor 时不起作用(不显示边框)。

enter image description here

最佳答案

对于 Swift:

enter image description here

swift 3:

extension UIView {

    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }

    @IBInspectable var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable var borderColor: UIColor? {
        get {
            return UIColor(cgColor: layer.borderColor!)
        }
        set {
            layer.borderColor = newValue?.cgColor
        }
    }
}

swift 2.2:

extension UIView {

    @IBInspectable var cornerRadius: CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }

    @IBInspectable var borderWidth: CGFloat {
        get {
            return layer.borderWidth
        }
        set {
            layer.borderWidth = newValue
        }
    }

    @IBInspectable var borderColor: UIColor? {
        get {
            return UIColor(CGColor: layer.borderColor!)
        }
        set {
            layer.borderColor = newValue?.CGColor
        }
    }
}

关于ios - 更改 Storyboard 中的 UIButton BorderColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28854469/

相关文章:

ios - 从 application(.... launchOptions : [NSObject: AnyObject]? ) 处的 launchOptions 获取值

iphone - 如何从一个 View Controller 导航到另一个 View Controller ?

r - igraph 图条件顶点颜色

android自定义颜色进度条样式

iOS - 在 swift 中使用 ObjectMapper 映射根 JSON 数组

ios - 使用带有 UIViewController 容器的 UISearchDisplayController

Git 颜色 : How do I set colors for the upstream branch in “git branch -vv” ?

ios - 如何检查 UIButton 的 titleLabel 是否超过按钮的框架?

ios - 添加渐变的 UIButton 被切断

iphone - 我如何从另一个方法调用这个方法?