ios 在 Storyboard 中更改 UIButton BorderColor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28854469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change UIButton BorderColor in Storyboard
提问by Mohamed Jaleel Nazir
I Set CornerRadius and BorderWidth for UIbutton in User Defined Runtime Attributes. Without adding layer.borderColorit works Well and Display border in black color. But when add layer.borderColordoes not work(does not show border).
我在用户定义的运行时属性中为 UIbutton 设置了 CornerRadius 和 BorderWidth。不添加layer.borderColor它运作良好并以黑色显示边框。但是当添加layer.borderColor不起作用(不显示边框)。
回答by Mohamed Jaleel Nazir
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
}
}
}
回答by Mohamed Jaleel Nazir
I got Answer Change layer.borderColorto layer.borderColorFromUIColor
我得到了答案更改 layer.borderColor到 layer.borderColorFromUIColor
and add the code in .m file
并在 .m 文件中添加代码
#import <QuartzCore/QuartzCore.h>
@implementation CALayer (Additions)
- (void)setBorderColorFromUIColor:(UIColor *)color
{
self.borderColor = color.CGColor;
}
@end
Tickproperties in Attribute Inspector
在属性检查器中勾选属性
回答by Cons Bulaquena
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- 使用IBDesignable
和IBInspectable
构建自定义控件并在 Interface Builder 中实时预览设计。
Here is a sample code in Swift, place just below the UIKit
in ViewController.swift:
这是 Swift 中的示例代码,放在UIKit
ViewController.swift 的正下方:
@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.
在构建时间运行,瞧!您将看到带边框的清晰圆形按钮。
回答by pkamb
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 的用户定义的运行时属性设置类型!
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
),并在类别中覆盖相应的设置器(setBorderColorFromUIColor:
)。
回答by cdeerinck
There is a much better way to do this! You should use @IBInspectable. Check out Mike Woelmer's blog entry here: https://spin.atomicobject.com/2017/07/18/swift-interface-builder/
有一个更好的方法来做到这一点!您应该使用@IBInspectable。在此处查看 Mike Woelmer 的博客条目:https://spin.atomicobject.com/2017/07/18/swift-interface-builder/
It actually adds the feature to IB in Xcode! 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. But following his post will add them.
它实际上将功能添加到 Xcode 中的 IB!其他答案中的一些屏幕截图使它看起来好像这些字段存在于 IB 中,但至少在 Xcode 9 中它们不存在。但是按照他的帖子会添加它们。
回答by Jovan Stankovic
This works for me.
这对我有用。
Swift 3, Xcode 8.3
斯威夫特 3,Xcode 8.3
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)
}
}
}
}
回答by Furqan Khan
In case of Swift, function doesn't work. You'll need a computed property to achieve the desired result:
在 Swift 的情况下,函数不起作用。您需要一个计算属性来实现所需的结果:
extension CALayer {
var borderColorFromUIColor: UIColor {
get {
return UIColor(CGColor: self.borderColor!)
} set {
self.borderColor = newValue.CGColor
}
}
}