无法在故事板上将自定义IBDesignable类链接到UIButton

时间:2017-07-26 08:28:05

标签: ios swift uibutton

如何将IBDesignable自定义类连接到UIButton?

代码

public var leftAction: Selector? {
    didSet {
        if (leftAction != nil){
            self.leftButton.addTarget(self, action: leftAction!, for: .touchUpInside)
        }
    }
}

该文件的名称为import UIKit @IBDesignable class MyButton: UIView { @IBInspectable var borderColor: UIColor = UIColor.clear { didSet { layer.borderColor = borderColor.cgColor } } @IBInspectable var borderWidth: CGFloat = 0 { didSet { layer.borderWidth = borderWidth } } @IBInspectable var cornerRadius: CGFloat = 0 { didSet { layer.cornerRadius = cornerRadius } } }

enter image description here

它没有将MyButton显示为班级?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:5)

你可能需要MyButton继承UIButton,而不是UIView。

答案 1 :(得分:1)

您只能将自定义类链接到相同类型的对象或它继承的类型。而不是

@IBDesignable class MyButton: UIView {
你可能想要

@IBDesignable class MyButton: UIButton {

或将其链接到UIView对象