How to create an IBInspectable of type enum

前端 未结 6 1439
遇见更好的自我
遇见更好的自我 2020-12-04 11:56

enum is not an Interface Builder defined runtime attribute. The following does not show in Interface Builder\'s Attributes Inspector:



        
6条回答
  •  时光取名叫无心
    2020-12-04 12:28

    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
    

提交回复
热议问题