在Storyboard中为UIButton设置边框

27

在Xcode 5中,如果不直接在代码中设置,我无法为我的按钮添加边框。是否可能在Storyboard上没有办法实现这一点,而必须制作自定义背景图像?

5个回答

76

您可以使用键路径。

例如,如图所示描述的圆角半径(layer.cornerRadius)。 在 storyboard 上看不到效果,因为这些参数在运行时计算。现在,您可以在 UIView 中使用 Swift 类别(图片下方的代码),并使用 @IBInspectable 在 storyboard 中显示结果(如果您正在使用该类别,请仅使用 cornerRadius 而不是 layer.cornerRadius 作为键路径。

enter image description here


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

这是来自Peter DeWeese的答案中提供的类别,它允许使用键路径layer.borderUIColor来设置边框颜色。

CALayer+XibConfiguration.h:

#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>

@interface CALayer(XibConfiguration)

// This assigns a CGColor to borderColor.
@property(nonatomic, assign) UIColor* borderUIColor;

@end

CALayer + XibConfiguration.m:

#import "CALayer+XibConfiguration.h"

@implementation CALayer(XibConfiguration)

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

-(UIColor*)borderUIColor
{
    return [UIColor colorWithCGColor:self.borderColor];
}

@end

3
每天都可以学到新东西,这个知识在几个月前就知道该有多好啊哈哈。 - RyanG
2
哇,这太酷了!我不知道有这个隐藏的宝藏!点赞。 - TotoroTotoro
3
顺便提一下,我还需要设置layer.borderWidth才能显示边框。 - TotoroTotoro
1
这是因为layer.borderColor期望的是一个CGColor,而不是UIColor。但你可以创建一个类别作为解决方法。等一下,我会更新我的答案。 - Guilherme Torres Castro
@GuilhermeTorresCastro 我对iOS开发还有点陌生。边框颜色片段是在单独的文件中吗?一旦实现了,如何使用它? - Noah Passalacqua
显示剩余11条评论

9

Swift 3 如果您想在使用IBInspectable时在IB中查看结果,则必须扩展UIView并将属性添加到该类中,即:

@IBDesignable class MyView: UIView {}

extension MyView {
    @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
            layer.masksToBounds = newValue > 0
        }
    }

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

参考资料:http://nshipster.com/ibinspectable-ibdesignable/

IBInspectable和IBDesignable是一些有用的工具,可以在Interface Builder中实时显示视图的外观。IBInspectable允许在IB中设置属性,而IBDesignable允许在代码中呈现可视化效果。这使得开发人员更容易调整和改进设计和用户体验。

0

简短回答:

layer.cornerRadius = 10
layer.borderWidth = 1
layer.borderColor = UIColor.blue.cgColor

长答案:

圆角 UIButton

customUIView.layer.cornerRadius = 10

边框厚度

pcustomUIView.layer.borderWidth = 1

边框颜色

customUIView.layer.borderColor = UIColor.blue.cgColor

0

您可以设置UIButton的用户定义运行时属性,例如borderWidth、cornerRadius、borderColor等。如图所示。 注意:不要在属性名称前使用layer.,否则它将无法工作。

enter image description here


2
尝试过了,不起作用,你能提供一个链接来说明为什么这应该起作用吗?或者给个例子。我猜你的意思是需要先编写带有IBInspectable变量的扩展,就像其他答案中所述。回答需要详细些。 - Zaporozhchenko Oleksandr

-3
你可以使用以下代码片段: self.addButton.layer.borderColor = [[UIColor greenColor] CGColor]; 请注意:addButton 是一个 IBOutlet。

提问者需要一种非编程的方式。 - AbaEesa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接