How to create an IBInspectable enum type

not enum , created by the Interface Builder runtime attribute. The Interface Interface Attributes Inspector does not display the following:

 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 supported by Interface Builder runtime attributes: boolean, integer or floating point number, string, localized string, rectangle, dot, size, color, range and zero.

Q: How can I see enum in the attribute inspector of the Builder interface?

+71
ios xcode swift interface-builder
Dec 11 '14 at 21:30
source share
7 answers

Swift 3

@IBInspectable var shape:StatusShape = .Rectangle just creates an empty entry in Interface Builder:

Not available in IB

Use an adapter that acts as a bridge between Swift and Interface Builder.
shapeAdapter checked with IB:

  // IB: use the adapter @IBInspectable var shapeAdapter:Int { get { return self.shape.rawValue } set( shapeIndex) { self.shape = StatusShape(rawValue: shapeIndex) ?? .Rectangle } } 

Available in IB

Unlike the conditional compilation approach (using #if TARGET_INTERFACE_BUILDER ), the shape variable does not change for the purpose, potentially requiring further changes to the source code to deal with shape:NSInteger options shape:NSInteger vs. shape:StatusShape :

  // Programmatically: use the enum var shape:StatusShape = .Rectangle 

Full 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 .

+67
Dec 12 '14 at 1:08
source share

Instead of setting your verified enums with ints, you can also set them with strings. Although not as preferable as a drop-down list, at least this option offers some degree of readability.

For Swift only:

 // 1. Set up your enum enum Shape: String { case Rectangle = "rectangle" // lowercase to make it case-insensitive case Triangle = "triangle" case Circle = "circle" } // 2. Then set up a stored property, which will be for use in code var shape = Shape.Rectangle // default shape // 3. And another stored property which will only be accessible in IB (because the "unavailable" attribute prevents its use in code) @available(*, unavailable, message: "This property is reserved for Interface Builder. Use 'shape' instead.") @IBInspectable var shapeName: String? { willSet { // Ensure user enters a valid shape while making it lowercase. // Ignore input if not valid. if let newShape = Shape(rawValue: newValue?.lowercased() ?? "") { shape = newShape } } } 

You can also get this to work with objective-c by adding an initializer to the enumeration. However, the compiler will only show an “inaccessible” error for your IB-only properties in fast code.

Swift Option with Obj-C Compatibility:

 @objc enum Shape: Int { case None case Rectangle case Triangle case Circle init(named shapeName: String) { switch shapeName.lowercased() { case "rectangle": self = .Rectangle case "triangle": self = .Triangle case "circle": self = .Circle default: self = .None } } } var shape = Shape.Rectangle // default shape @available(*, unavailable, message: "This property is reserved for Interface Builder. Use 'shape' instead.") @IBInspectable var shapeName: String? { willSet { if let newShape = Shape(rawValue: newValue?.lowercased() ?? "") { shape = newShape } } } 
+33
Aug 24 '15 at 23:31
source share

I can’t remember the quick syntax, but that’s how I solved it in obj-c

 #if TARGET_INTERFACE_BUILDER @property (nonatomic) IBInspectable NSInteger shape; #else @property (nonatomic) StatusShape shape; #endif 
+20
Mar 05 '15 at 23:16
source share

This is an old thread, but useful. I adapted my answer to fast 4.0 and Xcode 9.0 - Swift 4 has its own minor problems with this problem. I have an @IBInspectable variable with an enumeration type, and Xcode 9.0 is unhappy, showing me that this property cannot be marked as @IBInspectable because its type cannot be represented in Objective-c "

@Eporediese partially answers this problem (for swift3); using a property for a storyboard, but a direct enumeration for the rest of the code. Below is a more complete set of codes, which gives you the property to work in both cases.

 enum StatusShape: Int { case Rectangle = 0 case Triangle = 1 case Circle = 2 } var _shape:StatusShape = .Rectangle // this is the backing variable #if TARGET_INTERFACE_BUILDER @IBInspectable var shape: Int { // using backing variable as a raw int get { return _shape.rawValue } set { if _shape.rawValue != newValue { _shape.rawValue = newValue } } } #else var shape: StatusShape { // using backing variable as a typed enum get { return _shape } set { if _shape != newValue { _shape = newValue } } } #endif 
+3
Oct 13 '17 at 22:33
source share

You can not. If you want it to be available in Interface Builder, make your property one of the supported types. In this case, make it int and apply it to the enum type in the code.

+1
Dec 11 '14 at 21:59
source share

Swift 3 solution based on SwiftArchitect

 enum StatusShape: Int { case rectangle, triangle, circle } var statusShape: StatusShape = .rectangle #if TARGET_INTERFACE_BUILDER @IBInspectable var statusShapeIB: Int { get { return statusShape.rawValue } set { guard let statusShape = StatusShape(rawValue: newValue) else { return } self.statusShape = statusShape } } //convenience var, enum not inspectable #endif 
+1
Mar 09 '17 at 9:50
source share
 I just removed @IBInspectable keyword //MARK:- Not confirm its will work for other or not but easy way once you can try it 

where did this error come from (for me it was twice). This will require you to unlock the VHBoomMenuButton library so that you can make changes.

 and its work 
-one
May 13 '19 at 5:34
source share



All Articles