【问题标题】:Set a border for UIButton in Storyboard在 Storyboard 中为 UIButton 设置边框
【发布时间】:2013-12-26 23:55:57
【问题描述】:

如果不直接在代码中设置它们,我无法在 Xcode 5 中为我的按钮设置边框。如果不制作自定义背景图像,是否有可能无法在情节提要上执行此操作?

【问题讨论】:

    标签: ios objective-c swift uibutton storyboard


    【解决方案1】:

    您可以使用密钥路径。

    例如图片上描述的圆角半径 (layer.cornerRadius)。 您将无法在故事板上看到效果,因为此参数是在运行时评估的。 现在您可以在 UIView 中使用 swift 类别(图片下方的代码)与@IBInspectable 一起显示故事板上的结果(如果您使用类别,请仅使用 cornerRadius 而不是 layer.cornerRadius 作为关键路径。


    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
    

    【讨论】:

    • 你每天都在学习新的东西。几个月前知道这会很有用哈。
    • 哇,这太酷了!我不知道这个隐藏的宝石!赞成。
    • 顺便说一句,我还必须设置 layer.borderWidth 才能显示边框。
    • 太疯狂了!非常感谢 Guilherme,我有一种感觉,将来会派上用场
    • 这是因为 layer.boderColor 期望的是 CGColor,而不是 UIColor。但是您可以将类别作为解决方法。等一下,我会更新我的答案
    【解决方案2】:

    斯威夫特 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/

    【讨论】:

      【解决方案3】:

      简答:

      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
      

      【讨论】:

        【解决方案4】:

        您可以设置您的 UIButton 用户定义的运行时属性,即 borderWidth、cornerRadius、borderColor 等。如图所示。 注意: - 不要使用图层。在属性名称之前,它不会起作用。

        【讨论】:

        • 试过了,它不工作,你能提供一个文档链接,为什么这应该工作?或一个例子。我猜你的意思是你需要首先使用 IBInspectable vars 编写扩展,就像其他答案一样。答案必须是完整的
        【解决方案5】:

        您可以使用如下代码:

        self.addButton.layer.borderColor = [[UIColor greenColor] CGColor];

        请注意:addButton 是一个 IBOutlet。

        【讨论】:

        • 提问者需要非编程方式。
        猜你喜欢
        • 2016-07-26
        • 2019-11-30
        • 1970-01-01
        • 2016-06-21
        • 2013-05-15
        • 2022-01-14
        • 2010-10-25
        • 2013-09-17
        • 2014-11-27
        相关资源
        最近更新 更多