问题描述
枚举
不是Interface Builder定义的运行时属性。
以下内容未在Interface Builder的Attributes Inspector中显示:
enum StatusShape:Int {
case Rectangle = 0
case Triangle = 1
case Circle = 2
}
@IBInspectable var shape:StatusShape = .Rectangle
从文档:
您可以将IBInspectable属性附加到类声明,类扩展或类别中的任何属性,以支持任何类型的支持通过Interface Builder定义的运行时属性:布尔值,整数或浮点数,字符串,本地化字符串,矩形,点,大小,颜色,范围和零。
问:如何在Interface Builder的属性检查器中看到枚举
?
Swift 3
@IBInspectable var shape:StatusShape =。 Rectangle
只是在Interface Builder中创建一个空白条目:
使用适配器,它将充当桥梁 Swift和Interface Builder之间。
shapeAdapter
可从IB检查:
// IB:使用适配器
@IBInspectable var shapeAdapter:Int {
get {
return self.shape.rawValue
}
set (shapeIndex){
self.shape = StatusShape(rawValue:shapeIndex)?? .Rectangle
}
}
上。
enum
is not an Interface Builder defined runtime attribute.The following does not show in Interface Builder's Attributes Inspector:
enum StatusShape:Int {
case Rectangle = 0
case Triangle = 1
case Circle = 2
}
@IBInspectable var shape:StatusShape = .Rectangle
From the documentation:You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type that’s supported by the Interface Builder defined runtime attributes: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.
Q: How can I see an enum
in Interface Builder's Attributes Inspector?
Swift 3
@IBInspectable var shape:StatusShape = .Rectangle
merely creates a blank entry in Interface Builder:
Use an adapter, which will acts as a bridge between Swift and Interface Builder.shapeAdapter
is inspectable from IB:
// IB: use the adapter
@IBInspectable var shapeAdapter:Int {
get {
return self.shape.rawValue
}
set( shapeIndex) {
self.shape = StatusShape(rawValue: shapeIndex) ?? .Rectangle
}
}
Unlike the conditional compilation approach (using #if TARGET_INTERFACE_BUILDER
), the type of the shape
variable does not change with the target, potentially requiring further source code changes to cope with the shape:NSInteger
vs. shape:StatusShape
variations:
// Programmatically: use the enum
var shape:StatusShape = .Rectangle
Complete code
@IBDesignable
class ViewController: UIViewController {
enum StatusShape:Int {
case Rectangle
case Triangle
case Circle
}
// Programmatically: use the enum
var shape:StatusShape = .Rectangle
// IB: use the adapter
@IBInspectable var shapeAdapter:Int {
get {
return self.shape.rawValue
}
set( shapeIndex) {
self.shape = StatusShape(rawValue: shapeIndex) ?? .Rectangle
}
}
}
► Find this solution on GitHub.
这篇关于如何创建类型枚举的IBInspectable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!