@IBDesignable 不适用于 iOS swift 3
@IBDesignable not working in iOS swift 3
我做了什么:
删除派生数据,重新启动 xcode,编辑器->Refresh all views
当我点击编辑器->Refresh all views
时,故事板中出现 Designables
构建失败。
请检查以下代码。我错过了什么?当我从属性检查器更改 @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
}
}
- 将自定义 class 改回原来的样子(UITextField?)
- 仅将 xib 中的文件所有者设置为 BorderedFloatingTF
检查"Module"字段中指定的模块是否正确。现在指定"Template1",对吗?
还尝试将所有 @IBInspectable 属性设置为 public
尽量只留下这部分:
@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])
}
}
}
试试这个
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
都工作正常。
在我的项目中,我缺少设置它的值类型。你的问题不在于此。但对于其他和我有同样问题的人,我想解释一下。
示例:
我是这样写的:
@IBInspectable public var rowOrColoumnCount = 0
但它应该是这样的:
@IBInspectable public var rowOrColoumnCount : Int = 0
我在从 xib 加载自定义 class 时遇到了这个问题。最终偶然发现了这个解决方案(必须为 class 使用正确的包,而不是 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
}
}
我在一个项目中遇到了同样的问题。我执行了以下步骤来解决问题。
在自定义 IBDesignable 视图中,确保覆盖这两个方法
required init?(coder aDecoder: NSCoder)
override init(frame: CGRect)
将以下内容添加到构建设置中的运行路径搜索路径
@loader_path/Frameworks
$(CONFIGURATION_BUILD_DIR)
清理您的项目,删除派生数据。
应该没问题。
我做了什么:
删除派生数据,重新启动 xcode,编辑器->Refresh all views
当我点击编辑器->Refresh all views
时,故事板中出现 Designables
构建失败。
请检查以下代码。我错过了什么?当我从属性检查器更改 @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
}
}
- 将自定义 class 改回原来的样子(UITextField?)
- 仅将 xib 中的文件所有者设置为 BorderedFloatingTF
检查"Module"字段中指定的模块是否正确。现在指定"Template1",对吗? 还尝试将所有 @IBInspectable 属性设置为 public
尽量只留下这部分:
@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])
}
}
}
试试这个
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
都工作正常。
在我的项目中,我缺少设置它的值类型。你的问题不在于此。但对于其他和我有同样问题的人,我想解释一下。
示例: 我是这样写的:
@IBInspectable public var rowOrColoumnCount = 0
但它应该是这样的:
@IBInspectable public var rowOrColoumnCount : Int = 0
我在从 xib 加载自定义 class 时遇到了这个问题。最终偶然发现了这个解决方案(必须为 class 使用正确的包,而不是 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
}
}
我在一个项目中遇到了同样的问题。我执行了以下步骤来解决问题。
在自定义 IBDesignable 视图中,确保覆盖这两个方法
required init?(coder aDecoder: NSCoder)
override init(frame: CGRect)
将以下内容添加到构建设置中的运行路径搜索路径
@loader_path/Frameworks
$(CONFIGURATION_BUILD_DIR)
清理您的项目,删除派生数据。
应该没问题。