【问题标题】:@IBDesignable not working in iOS swift 3@IBDesignable 在 iOS swift 3 中不起作用
【发布时间】:2017-05-17 11:42:51
【问题描述】:

我做了什么: 删除派生数据,重启xcode,编辑器->Refresh all views

当我单击 Editor->Refresh all views 时,我在情节提要中收到 Designables build failed。

请检查以下代码。我错过了什么?当我从属性检查器更改 @IBInspectable 值时,情节提要中的 Textfiled 没有得到更新。

import UIKit
import QuartzCore

@IBDesignable
class BorderedFloatingTF: UITextField {


    required init?(coder aDecoder:NSCoder) {
        super.init(coder:aDecoder)
        setup()
    }

    override init(frame:CGRect) {
        super.init(frame:frame)
        setup()
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        setup()
    }

    // properties..

    @IBInspectable var enableTitle : Bool = false
    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }
    @IBInspectable var borderWidth: Int = 1 {
        didSet {
            layer.borderWidth = CGFloat(borderWidth)
        }
    }
    @IBInspectable var placeHolderColor: UIColor = UIColor.white {

        didSet {
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
        }
    }

    fileprivate func setup() {
        borderStyle = UITextBorderStyle.none
        layer.borderWidth = CGFloat(borderWidth)
        layer.borderColor = borderColor.cgColor
        placeHolderColor = UIColor.white
    }
}

【问题讨论】:

  • 有什么问题?我第一次这样做。
  • 我完全测试了你的代码,它在我的系统中运行良好,在这里我附上了与你的代码相同的快照。重新启动您的 xcode 并查看。 Xcode 可能存在内存相关问题。
  • 第一步是在Interface Builder崩溃日志中找到崩溃日志并查看堆栈跟踪。

标签: ios swift xcode ibdesignable


【解决方案1】:

试试这个

import QuartzCore

@IBDesignable
class BorderedFloatingTF: UITextField {


    required init?(coder aDecoder:NSCoder) {
        super.init(coder:aDecoder)
        setup()
    }

    override init(frame:CGRect) {
        super.init(frame:frame)
        setup()
    }

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: 20, dy: 0)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return textRect(forBounds: bounds)
    }


    // properties..

    @IBInspectable var enableTitle : Bool = false
    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }
    @IBInspectable var borderWidth: Int = 1 {
        didSet {
            layer.borderWidth = CGFloat(borderWidth)
        }
    }
    @IBInspectable var placeHolderColor: UIColor = UIColor.white {

        didSet {
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
        }
    }

    func setup() {
        borderStyle = UITextBorderStyle.none
        layer.borderWidth = CGFloat(borderWidth)
        layer.borderColor = borderColor.cgColor
        placeHolderColor = UIColor.white
    }
}

您的IBDesignablesIBInspectables 都工作正常。

【讨论】:

  • 感谢您的努力@krunal。我创建了一个新项目,这里的代码运行良好。
【解决方案2】:

我在从 xib 加载自定义类时遇到了这个问题。最后偶然发现了这个解决方案(必须为类使用正确的包,而不是 Bundle.main):

@IBDesignable
class DataEntryView: UIView {

@IBOutlet var contentView: DataEntryView!

@IBInspectable var hasError : Bool = false

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.commonInit()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.commonInit()
}

private func commonInit() {
    let bundle = Bundle.init(for: type(of: self))
    bundle.loadNibNamed("DataEntryView", owner: self, options: nil)
    addSubview(contentView)
    contentView.translatesAutoresizingMaskIntoConstraints = false
    contentView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    contentView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
    contentView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
    contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
}

【讨论】:

  • 你是救生员兄弟.. ??‍♂️ 试图找出没有渲染这么久的原因..
【解决方案3】:

我在一个项目中遇到了同样的问题。我执行了以下步骤来解决此问题。

在自定义 IBDesignable 视图中,确保覆盖这两个方法

required init?(coder aDecoder: NSCoder)
override init(frame: CGRect)

将以下内容添加到构建设置中的运行路径搜索路径

@loader_path/Frameworks
$(CONFIGURATION_BUILD_DIR)

清理您的项目,删除派生数据。

应该没问题。

【讨论】:

  • 我的自定义控件从未在 IB 中呈现,在添加了您提到的 init 方法后它开始工作。我没有添加运行脚本。你是我的英雄,谢谢!
【解决方案4】:

在我的项目中,我缺少设置它的值类型。你的问题不在于这个。但是对于和我有同样问题的其他人,我想解释一下。

示例: 我是这样写的:

@IBInspectable public var rowOrColoumnCount = 0

但它应该是这样的:

@IBInspectable public var rowOrColoumnCount : Int = 0

【讨论】:

    【解决方案5】:

    检查“模块”字段中指定的模块是否正确。现在指定为“Template1”,对吗? 也尝试将所有 @IBInspectable 属性设为公开

    尽量只留下这部分:

    @IBDesignable
    class BorderedFloatingTF: UITextField {
    
    // properties..
    
    @IBInspectable var enableTitle : Bool = false
    @IBInspectable var borderColor: UIColor = UIColor.white {
        didSet {
            layer.borderColor = borderColor.cgColor
        }
    }
    @IBInspectable var borderWidth: Int = 1 {
        didSet {
            layer.borderWidth = CGFloat(borderWidth)
        }
    }
    @IBInspectable var placeHolderColor: UIColor = UIColor.white {
    
        didSet {
            self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeHolderColor])
        }
    }
    

    }

    【讨论】:

    • 它没有帮助。如问题中所述,我仍然无法构建。
    • 检查“模块”字段中指定的模块是否正确。现在指定为“Template1”,对吗?也尝试将所有@ibinspectable 属性设为公开
    【解决方案6】:
    1. 将自定义类改回原来的样子(UITextField?)
    2. 仅将 xib 中的 File's Owner 设置为 BorderedFloatingTF

    【讨论】:

    • 我正在使用情节提要。如果我不设置自定义类,代码如何工作?
    • 我假设您在 Storyboard 中也有一个相应的 xib,称为 BorderedFloatingTF。在这种情况下,您指的是自身内部的类。当您使用 BorderedFloatingTF 时,例如在 viewcontorller 中。在那里你可以拖动一个常规的 textField 并将它的类设置为你的自定义类。
    • 如果可以的话,你能分享一下你的项目吗?我无法想象项目结构是什么样的。
    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多