How to create an IBInspectable of type enum

ibinspectable swift 4 dropdown
ibdesignable
ibinspectable supported types
property cannot be marked @ibinspectable because its type cannot be represented in objective-c
ibinspectable corner radius
ibinspectable font swift
ibinspectable custom font
ibinspectable properties

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.

Why I can't use IBInspectable with enumeration?, "Use enum to create an enumeration. Like classes and all other named types, enumerations can have methods associated with them." [2]. I am having an @IBInspectable variable with enum type and Xcode 9.0 is not happy, showing me this "Property cannot be marked @IBInspectable because its type cannot be representing in Objective-c" @Eporediese answers this problem (for swift3) in part; using a property for the storyboard but a straight enum for the rest of the code.

Instead of setting your inspectable enums with ints, you could also set them with strings. Although not quite as preferable as a dropdown, at least this option offers some level of readability.

Swift-only Option:

// 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
        }
    }
}

It is possible to also get this to work with objective-c as well, by adding an initializer to the enum. However, the compiler will only show the "unavailable" error for your IB-only properties in swift 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
        }
    }
}

How to create an IBInspectable of type enum, enum is not an Interface Builder defined runtime attribute. The following does not show in Interface Builder's Attributes Inspector: enum StatusShape:Int { case� C# Enumerations Type - Enum. In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week. An enum is defined using the enum keyword, directly inside a namespace

I can't remember the swift syntax, but this is how I solved it in obj-c

#if TARGET_INTERFACE_BUILDER
@property (nonatomic) IBInspectable NSInteger shape;
#else
@property (nonatomic) StatusShape shape;
#endif

@IBInspectable enums, It would be nice to be able to make swift enum variables @IBInspectable. In the case of enums without associated values, I would imagine that it would just make � C# Enums. An enum is a special "class" that represents a group of constants (unchangeable/read-only variables).. To create an enum, use the enum keyword (instead of class or interface), and separate the enum items with a comma:

This is an old thread but useful. I have adapted my answer to swift 4.0 and Xcode 9.0 - Swift 4 has its own little issues with this problem. I am having an @IBInspectable variable with enum type and Xcode 9.0 is not happy, showing me this "Property cannot be marked @IBInspectable because its type cannot be representing in Objective-c"

@Eporediese answers this problem (for swift3) in part; using a property for the storyboard but a straight enum for the rest of the code. Below is a more complete code set that gives you a property to work with 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

How to create an IBInspectable of type enum, enum is not an Interface Builder defined runtime attribute. The following does not show in Interface Builder's Attributes Inspector: enum StatusShape:Int { case� Working with enums in Java 10 you have many options of creating and using enums. Which one to use depends on your needs: * create simple enum and get ordinal * create enum with code field (instance field) * access values by FOR loop * Iterate with EnumSet and forEach * Iterate enum with Stream * All examples and filter enum values More info on enums from Java docs: * Class Enum [https://docs

For 2020 - @SwiftArchitect answere updated for today

Here's a typical full example with all of today's syntax

import UIKit

@IBDesignable class ClipLabels: UILabel {
    
    enum Side: Int { case left, right }
    
    var side: Side = .left {
        didSet {
            common()
        }
    }
    
    @available(*, unavailable, message: "IB only")
    @IBInspectable var leftRight01: Int {
        get {
            return self.side.rawValue
        }
        set(index) {
            self.side = Side(rawValue: index) ?? .left
        }
    }
    

and just an example of use ...

switch side {
    case .left:
        textColor = .red
    case .right:
        textColor = .green
    }

For this critical Swift/iOS QA,

• the very old answer of @SwiftArchitect is perfectly correct but

• I've just updated it and added the critical "unavailable" thing, which is now possible in Swift.

@IBInspectable with enum?, How to create an IBInspectable of type enum, From the documentation: You can attach the IBInspectable attribute to any property in a class declaration, class� The enum declaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a static values method that returns an array containing all of the values of the enum in the order they are declared. This

ios: How to create an IBInspectable of type enum, enum is not an Interface Builder defined runtime attribute.The following does not show in Interface Builder's Attributes Inspector:enum� enum_name − Any name given by user. const1, const2 − These are values of type flag. The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows − enum colors{red, black}; enum suit{heart, diamond=8, spade=3, club}; The following is an example of enums. Example

ios - @IBInspectable con enum?, � possibile collegare l'attributo IBInspectable a qualsiasi propriet� in una dichiarazione di classe, _someThirdFlag, @"Only one flag can be set"); } }. Questo � Use the Enum.IsDefined method to determine whether an enumeration type contains an enum member with the certain associated value. For any enumeration type, there exist boxing and unboxing conversions to and from the System.Enum type, respectively. C# language specification. For more information, see the following sections of the C# language

rdar://32443199: IBInspectable doesn't support enum , IBInspectable doesn't support enum �\_(ツ)_/� Summary: It's well known that IBInspectable only supports the following types: Int CGFloat (Ex: using NSInteger or NSString) Steps to Reproduce: 1- Create a class called� Type. An enum has an underlying type. Each time we use the enum, we are using the underlying type. The enum has syntactic sugar on top. Int: Enums are by default an int type, but we can adjust this to a different numeric type. Byte: Here we create an enum with a type of byte. This is sometimes useful on small enums.

Comments
  • Where is enum in that list? Why would you think you can use an enum?
  • It would be nice to be able to pick an enum case straight from IB I suppose, or a UIFont, like the native UIKit objects can.
  • Why are you keeping shapeAsInt as a stored property rather than a computed property?
  • Right. You can't make a property writeonly though... but a computed property doesn't create a backing instance variable like your stored property does.
  • Added a no backing store solution proposed by @nhgrif.
  • Excuse my downvote. I thought I had a working solution with TARGET_INTERFACE_BUILDER but I was misled. Sorry, best of luck here on SO
  • fast forward to 2017 does this issue still remain the same?
  • I like the Swift Only version: it allows for plain English in IB.
  • Does anyone know how to get the options presented as a dropdown of strings in IB using this method?
  • I would like to know if possible how to get this done using a dropdown please.
  • The only place that I have the conditional assembly is that one spot in the declaration. No conditional assembly anywhere else since an enum is an integer value in obj-c. One spot to remove when apple fixes this (assuming that they will.)
  • This is a witty and awesome solution, and mentioned here nshipster.com/ibinspectable-ibdesignable
  • If you wrap this IB-inspectable property in a #if TARGET_INTERFACE_BUILDER block it will only affect the rendering in Interface Builder, not at run time. This will likely result in unexpected behavior and you'll always get a warning in the console: Failed to set (statusShapeIB) user defined inspected property ...: this class is not key value coding-compliant for the key statusShapeIB.