本文介绍了Swift 在 Storyboard 中设置 UIButton setBorderColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Storyboard 上设置 UIButton 边框颜色:

I'm trying to set UIButton Border color on Storyboard:

但是按钮仍然是黑色的.当我尝试使用 layer.borderColor 时,它根本不显示边框.

But the button is still Black. When I try to use layer.borderColor, it doesn't show the border at all.

如何设置颜色?

谢谢

//更新

我已将基于@dfd 的设置方式更改为:

I've changed the way to set up based on @dfd to:

import Foundation
import UIKit

@IBDesignable

public class OnboardingButton: UIButton {
    @IBInspectable public var borderColor:UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
   }
   @IBInspectable public var borderWidth:CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
   }
   @IBInspectable public var cornerRadius:CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
   }
}

在 Storyboard 自定义类中设置到 *OnboardingButton 后,应用开始构建但失败.日志中没有错误.我在哪里可以找到错误以及如何修复它?

After set up in Storyboard Custom Class to *OnboardingButton app started build but Faield. There is no error in the Log. Where can I find the error and how to fix it?

推荐答案

这是我的 UIButton 子类:

Here's my UIButton subclass:

@IBDesignable
public class Button: UIButton {
    @IBInspectable public var borderColor:UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }
    @IBInspectable public var borderWidth:CGFloat = 0 {
        didSet {
        layer.borderWidth = borderWidth
        }
    }
    @IBInspectable public var cornerRadius:CGFloat {
        get {
            return layer.cornerRadius
        }
        set {
            layer.cornerRadius = newValue
            layer.masksToBounds = newValue > 0
        }
    }
}

我创建了一个简单的项目并添加了上面的 IBDesignable 子类.下面是结果的三个屏幕截图.请注意,在身份检查器中,我将类设置为 Button,而不是 UIButton,并且它报告 Designables 是最新的".请注意,在属性检查器中,这些作为属性显示在最顶部.

I created a simple project and added the above IBDesignable subclass. Below are three screen shots of the result. Note that in the Identity inspector I've set the Class to be Button, not UIButton and that it reports that the Designables are "Up to date". Note that in the Attributes inspector these appear as properties at the very top.

这篇关于Swift 在 Storyboard 中设置 UIButton setBorderColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:10