无法在自定义视图的 Storyboard 中看到 IBInspectable 自定义属性 class
Unable to see the IBInspectable custom properties in Storyboard of the custom view class
我创建了自定义 UIView class,其中启用了 IBDesignable 方法。
在 class 中,我添加了一些 IBInspectable 属性,看起来像这样,
Swift 4.0 - 片段:
@IBDesignable
class SegmentView: UIView {
@IBInspectable var borderWidth = 0.0 {
didSet{
layer.borderWidth = CGFloat(borderWidth)
}
}
@IBInspectable var borderColor = UIColor.clear {
didSet{
layer.borderColor = borderColor.cgColor
}
}
}
每当我在 StoryBoard 中为 UIView 元素使用此 class 时,我无法在 Storyboard -> View -> View 属性 选项卡中查看上面声明的 IBInspectable 属性,以供参考,请检查 below-attached截图。
问题: 在上面的段视图 header 中,没有可用的 IBInspectable 属性。
有人可以帮我解决我的上述问题吗?
提前致谢。
需要显式声明变量类型,如:
@IBDesignable
class SegmentView: UIView {
@IBInspectable var borderWidth: CGFloat = 0.0 {
didSet {
layer.borderWidth = CGFloat(borderWidth)
}
}
@IBInspectable var borderColor: UIColor = .clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}
}
请将您的变量替换为以下即可解决您的问题。
@IBInspectable
public var borderWidth: CGFloat = 0.0{
didSet{
self.layer.borderWidth = self.borderWidth
}
}
@IBInspectable
public var borderColor: UIColor = UIColor.clear{
didSet{
self.layer.borderColor = self.borderColor.cgColor
}
}
我创建了自定义 UIView class,其中启用了 IBDesignable 方法。 在 class 中,我添加了一些 IBInspectable 属性,看起来像这样,
Swift 4.0 - 片段:
@IBDesignable
class SegmentView: UIView {
@IBInspectable var borderWidth = 0.0 {
didSet{
layer.borderWidth = CGFloat(borderWidth)
}
}
@IBInspectable var borderColor = UIColor.clear {
didSet{
layer.borderColor = borderColor.cgColor
}
}
}
每当我在 StoryBoard 中为 UIView 元素使用此 class 时,我无法在 Storyboard -> View -> View 属性 选项卡中查看上面声明的 IBInspectable 属性,以供参考,请检查 below-attached截图。
问题: 在上面的段视图 header 中,没有可用的 IBInspectable 属性。
有人可以帮我解决我的上述问题吗?
提前致谢。
需要显式声明变量类型,如:
@IBDesignable
class SegmentView: UIView {
@IBInspectable var borderWidth: CGFloat = 0.0 {
didSet {
layer.borderWidth = CGFloat(borderWidth)
}
}
@IBInspectable var borderColor: UIColor = .clear {
didSet {
layer.borderColor = borderColor.cgColor
}
}
}
请将您的变量替换为以下即可解决您的问题。
@IBInspectable
public var borderWidth: CGFloat = 0.0{
didSet{
self.layer.borderWidth = self.borderWidth
}
}
@IBInspectable
public var borderColor: UIColor = UIColor.clear{
didSet{
self.layer.borderColor = self.borderColor.cgColor
}
}