@IBDesignable无法在iOS swift 3中运行

时间:2017-05-17 11:42:51

标签: ios swift xcode ibdesignable

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

当我点击编辑器 - > Designables时,我在故事板中遇到Refresh all views版本失败。

enter image description here

请检查以下代码。我错过了什么?当我从属性检查器更改@IBInspectable值时,未在Storyboard中更新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
    }
}

6 个答案:

答案 0 :(得分:7)

试试这个

![enter image description here

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
    }
}

您的IBDesignables& IBInspectables两者都运转良好。

答案 1 :(得分:5)

我从xib加载自定义类时出现此问题。最后偶然发现了这个解决方案(必须为类使用正确的bundle,而不是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
}
}

答案 2 :(得分:4)

我在项目上遇到了同样的问题。我做了以下步骤来解决问题。

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

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

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

@loader_path/Frameworks
$(CONFIGURATION_BUILD_DIR)

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

那应该没问题。

答案 3 :(得分:1)

在我的项目中,我因缺少设置值Type而失踪。你的问题与此无关。但对于与我有同样问题的其他人,我想解释一下。

实施例: 我的写作如下:

@IBInspectable public var rowOrColoumnCount = 0

但它应该是这样的:

@IBInspectable public var rowOrColoumnCount : Int = 0

答案 4 :(得分:0)

检查“模块”字段中指定的模块是否正确。它现在指定了“Template1”,它是否正确? 还要尝试将所有@IBInspectable属性设为public

尽量只留下这部分:

{{1}}

}

答案 5 :(得分:-1)

  1. 将自定义类更改回原来的类(UITextField?)
  2. 仅将xib中的文件所有者设置为BorderedFloatingTF