xcode @IBDesignable 在 iOS swift 3 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44023888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:59:12  来源:igfitidea点击:

@IBDesignable not working in iOS swift 3

iosswiftxcodeibdesignable

提问by abhimuralidharan

What I did:Deleted derived data, Restarted xcode, Editor->Refresh all views

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

I am getting Designablesbuild failed in storyboard when I click on Editor->Refresh all views.

我正在Designables建设中的故事板失败当我点击编辑- > Refresh all views

enter image description here

在此处输入图片说明

Please check the following code. What am I missing? Textfiled is not getting updated in the storyboard when I change the @IBInspectable value from the attributes inspector.

请检查以下代码。我错过了什么?当我从属性检查器更改 @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
    }
}

采纳答案by Krunal

Try this

尝试这个

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

Your IBDesignables& IBInspectablesboth are working fine.

IBDesignablesIBInspectables两者都工作正常。

回答by Anthony Scott

I had this problem with a custom class loading from a xib. Finally stumbled on this solution (had to use the correct bundle for the class, rather than Bundle.main):

我在从 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
}
}

回答by Ilker Baltaci

I had the same problem on a project. I did the following steps to fix the issue.

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

In the custom IBDesignable view, make sure that you override both methods

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

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

Add following to your Runpath Search Paths in Build Settings

将以下内容添加到 Build Settings 中的 Runpath Search Paths

@loader_path/Frameworks
$(CONFIGURATION_BUILD_DIR)

Clean your project, delete derived data.

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

That should be all fine.

那应该没问题。

回答by Ahmet Kazim Günay

In my project i was missing for setting its value Type. Your problem is not about that. But for other people who are having same issue with me, I wanted to explain.

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

Example: I was writing like below:

示例:我写如下:

@IBInspectable public var rowOrColoumnCount = 0

But It should have been like:

但它应该是这样的:

@IBInspectable public var rowOrColoumnCount : Int = 0

回答by Anton Novoselov

Check whether it's correct Module specified in "Module" field. It's specified "Template1" now, is it correct? try also to make all @IBInspectable properties as public

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

Try to leave only this part:

尽量只留下这部分:

@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])
    }
}

}

}

回答by TheAppMentor

  1. Change the custom class back to what it was (UITextField?)
  2. Only set the File's Owner in the xib to BorderedFloatingTF
  1. 将自定义类改回原来的样子(UITextField?)
  2. 仅将 xib 中的文件所有者设置为 BorderedFloatingTF