ios - xcode Interface Builder 未更新 IBDesignable 类

标签 ios xcode interface-builder ibdesignable

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

例如,UITextField 类如下所示:

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 屏幕截图中可以看出,唯一更新的元素是占位符文本的缩进。类中的其余自定义没有更新。

有什么想法吗?

附:当我在身份检查器中选择一个元素时,“Designables”字段为“最新”。

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

谢谢!
斯科特

最佳答案

我在 Reinier 的第二个链接和我之前学习的 Udemy 类(class)的帮助下解决了这个问题。

我在 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)])
    }
}

关于ios - xcode Interface Builder 未更新 IBDesignable 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471742/

相关文章:

ios - 我怎样才能让 UIButton 粘在 UIView 的底部

ios - 编辑ios后是否需要一次又一次地将我的框架添加到项目中?

ios - 带有 NSMutableArray 的表的奇怪行为

iphone - 更改设备时 iOS 模拟器无法正确运行应用程序

ios - Iphone 桌面 View

ios - 如何使用 Interface Builder 设置链接到另一个窗口的 UITableView

swift - 框架内的自定义控件在 Xcode 中不可配置或不可见

ios - 'self' 未通过 Analyzer 设置为 '[(super or self) init…]' 的结果时使用的实例变量

objective-c - 保存 UITableView 单元格附件的状态?

ios - 如何解决iOS 13中的 'showsSelectionIndicator'?