xcode Interface Builder 未更新 IBDesignable class

xcode Interface Builder not updating IBDesignable class

在我的项目中,我有几个基于 UITextField 和 UIButton 的自定义 class 文件。

例如,UITextField class 如下所示:

import UIKit

@IBDesignable class RoundedTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()

        layer.cornerRadius = 25
        layer.borderWidth = 2
        layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        clipsToBounds = true
        textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

        // Placeholder colour
        attributedPlaceholder = NSAttributedString(string: placeholder!, attributes: [NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])
    }

    // Placeholder text indent
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 5)
    }

    // Editing text indent
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 5)
    }
}

这是模拟器显示给我的正确版本: SimScreenshot

以下是 IB 向我展示的内容: IBScreenshot

正如您从我的 IB 屏幕截图中看到的那样,唯一更新的元素是占位符文本的缩进。 class 中的其余自定义未更新。

有什么想法吗?

P.s。当我 select 一个元素时,在身份检查器中, 'Designables' 字段是 "Up to date".

我已尝试清理、构建和重新启动 Xcode。

谢谢! 斯科特

我在 Reinier 的第二个 link 和我之前在 Udemy 上的课程的帮助下解决了这个问题。

我在 setupView() 函数中实现了自定义。我在 prepareForInterfaceBuilder()awakeFromNib()

中调用了这个
import UIKit

@IBDesignable class RoundedTextField: UITextField {

    override func awakeFromNib() {
        super.awakeFromNib()
        setupView()
    }

    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        self.setupView()
    }

    // Placeholder text indent
    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 5)
    }

    // Editing text indent
    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 5)
    }

    func setupView() {
        layer.cornerRadius = 25
        layer.borderWidth = 2
        layer.borderColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
        clipsToBounds = true
        textColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

        // Placeholder colour
        attributedPlaceholder = NSAttributedString(string: placeholder!, attributes: [NSAttributedStringKey.foregroundColor: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)])
    }
}

//它会更新Simulator和InterfaceBuilder

import UIKit
@IBDesignable class ViewExtended: UIView {
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setupView()
    }
    func setupView() {
        layer.borderColor = borderColor.cgColor
        layer.borderWidth = borderWidth
        layer.cornerRadius = cornerRadius
        layer.shadowOpacity = shadowOpacity
        layer.shadowRadius = shadowRadius
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        setupView()
    }
    @IBInspectable var borderColor: UIColor = UIColor.red {
        didSet {
            layer.borderColor = borderColor.cgColor
            setNeedsLayout()
        }
    }
    @IBInspectable var borderWidth: CGFloat = 1.0 {
        didSet {
            layer.borderWidth = borderWidth
            setNeedsLayout()
        }
    }
    @IBInspectable var cornerRadius: CGFloat = 10.0 {
        didSet {
            layer.cornerRadius = cornerRadius
            setNeedsLayout()
        }
    }
    @IBInspectable var shadowColor: UIColor = UIColor.black {
        didSet {
            layer.borderColor = borderColor.cgColor
            setNeedsLayout()
        }
    }
    @IBInspectable var shadowOpacity: Float = 0.7 {
        didSet {
            layer.shadowOpacity = shadowOpacity
            setNeedsLayout()
        }
    }
    @IBInspectable var shadowRadius: CGFloat = 4.0 {
        didSet {
            layer.shadowRadius = shadowRadius
            setNeedsLayout()
        }
    }
    @IBInspectable var shadowOffset: CGSize = CGSize(width: 3, height: 3) {
        didSet {
            layer.shadowOffset = shadowOffset
            setNeedsLayout()
        }
    }
}