简体   繁体   English

在 Storyboard 中更改 UIButton BorderColor

[英]Change UIButton BorderColor in Storyboard

I Set CornerRadius and BorderWidth for UIbutton in User Defined Runtime Attributes.我在用户定义的运行时属性中为 UIbutton 设置了 CornerRadius 和 BorderWidth。 Without adding layer.borderColor it works Well and Display border in black color.不添加layer.borderColor它运作良好并以黑色显示边框。 But when add layer.borderColor does not work(does not show border).但是当添加layer.borderColor不起作用(不显示边框)。

在此处输入图片说明

For Swift:对于斯威夫特:

在此处输入图片说明

Swift 3:斯威夫特 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:斯威夫特 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
        }
    }
}

I got answer.我得到了答案。 Change borderColor instead of layer.borderColor :更改borderColor而不是layer.borderColor

Xcode 片段

and add this code in .m file:并在 .m 文件中添加此代码:

#import <QuartzCore/QuartzCore.h>
@implementation CALayer (Additions)

- (void)setBorderColorFromUIColor:(UIColor *)color
{
    self.borderColor = color.CGColor;
}

@end

Tick properties in Attribute Inspector在属性检查器中勾选属性

属性检查器

Swift 4, Xcode 9.2 - Use IBDesignable and IBInspectable to build custom controls and live preview the design in Interface Builder. Swift 4, Xcode 9.2 - 使用IBDesignableIBInspectable构建自定义控件并在 Interface Builder 中实时预览设计。

Here is a sample code in Swift, place just below the UIKit in ViewController.swift:这是 Swift 中的示例代码,放在 ViewController.swift 中UIKit正下方:

@IBDesignable extension UIButton {

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

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

    @IBInspectable var borderColor: UIColor? {
        set {
            guard let uiColor = newValue else { return }
            layer.borderColor = uiColor.cgColor
        }
        get {
            guard let color = layer.borderColor else { return nil }
            return UIColor(cgColor: color)
        }
    }
}

If you go to the Attributes inspectable of the view, you should find these properties visually, edit the properties:如果您转到视图的 Attributes 检查项,您应该可以直观地找到这些属性,然后编辑这些属性:

在此处输入图片说明

The changes are also reflected in User Defined Runtime Attributes:更改也反映在用户定义的运行时属性中:

在此处输入图片说明

Run in build time and Voila!在构建时间运行,瞧! you will see your clear rounded button with border.您将看到带边框的清晰圆形按钮。

在此处输入图片说明

The explanation, perhaps being lost in some of the other answers here:解释,也许在这里的其他一些答案中丢失了:

The reason that this property is not being set is that layer.borderColor needs a value with type CGColor .未设置此属性的原因是layer.borderColor需要一个CGColor类型的值。

But only UIColor types can be set via Interface Builder's User Defined Runtime Attributes!但是只有UIColor类型可以通过 Interface Builder 的 User Defined Runtime Attributes 设置!

So, you must set a UIColor to a proxy property via Interface Builder, then intercept that call to set the equivalent CGColor to the layer.borderColor property.因此,您必须通过 Interface Builder 将 UIColor 设置为代理属性,然后拦截该调用以将等效的 CGColor 设置为layer.borderColor属性。

This can be accomplished by creating a Category on CALayer, setting the Key Path to a unique new "property" ( borderColorFromUIColor ), and in the category overriding the corresponding setter ( setBorderColorFromUIColor: ).这可以通过在 CALayer 上创建一个 Category 来实现,将 Key Path 设置为唯一的新“属性”( borderColorFromUIColor ),并在类别中覆盖相应的 setter ( setBorderColorFromUIColor: )。

There is a much better way to do this!有一个更好的方法来做到这一点! You should use @IBInspectable.您应该使用@IBInspectable。 Check out Mike Woelmer's blog entry here: https://spin.atomicobject.com/2017/07/18/swift-interface-builder/在此处查看 Mike Woelmer 的博客条目: https ://spin.atomicobject.com/2017/07/18/swift-interface-builder/

It actually adds the feature to IB in Xcode!它实际上将功能添加到 Xcode 中的 IB! Some of the screenshots in other answers make it appear as though the fields exist in IB, but at least in Xcode 9 they do not.其他答案中的一些屏幕截图使它看起来好像这些字段存在于 IB 中,但至少在 Xcode 9 中它们不存在。 But following his post will add them.但是按照他的帖子会添加它们。

In case of Swift, function doesn't work.在 Swift 的情况下,函数不起作用。 You'll need a computed property to achieve the desired result:您需要一个计算属性来实现所需的结果:

extension CALayer {
    var borderColorFromUIColor: UIColor {
        get {
            return UIColor(CGColor: self.borderColor!)
        } set {
            self.borderColor = newValue.CGColor
        }
    }
}

This works for me.这对我有用。

Swift 3, Xcode 8.3斯威夫特 3,Xcode 8.3

身份检查器(在这种情况下为 UIButton

CALayer extension: CA层扩展:

extension CALayer {
var borderWidthIB: NSNumber {
    get {
        return NSNumber(value: Float(borderWidth))
    }
    set {
        borderWidth = CGFloat(newValue.floatValue)
    }
}
var borderColorIB: UIColor? {
    get {
        return borderColor != nil ? UIColor(cgColor: borderColor!) : nil
    }
    set {
        borderColor = newValue?.cgColor
    }
}
var cornerRadiusIB: NSNumber {
    get {
        return NSNumber(value: Float(cornerRadius))
    }
    set {
        cornerRadius = CGFloat(newValue.floatValue)
    }
}

} }

You have set the data values for the radius and the width set to be a string, but it should properly be to be set to a number, not a string您已将半径和宽度的数据值设置为字符串,但应正确设置为数字,而不是字符串

When you get it working, this will not be visible while looking at the storyboard, but will be when the app is running unless you have taken steps to make it @IBDesigneable.当你让它工作时,在查看故事板时这将不可见,但会在应用程序运行时可见,除非你已采取措施使其@IBDesigneable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM