Search code examples
iosxcodeswiftstoryboardnested-class

How to specify nested custom view class?


Available nested classes SuperView and NestedView.

class SuperView : UIImageView {

    class NestedView : UIImageView {
        var text : String = "Nested View"
    }

    var text : String = "Super View"
    var nested : NestedView?

}

I would like to set for a UIImageView the property named "Custom Class Name" to value "NestedView" inside the inspector of the storyboard scene. But the Interface Builder couldn't find "NestedView" class.

enter image description here


Solution

  • At this time, I think that Interface Builder only recognizes the names of Objective-C classes. You can still make Interface Builder find a nested class with the @objc keyword:

    class SuperView: UIView {
      @objc(SVNestedView) class NestedView: UIImageView {
      }
    }
    

    Then, in Interface Builder, specify that th view is of class SVNestedView. Since Objective-C isn't namespaced, you still need to pick unique names for each nested class, but at least the Swift side is properly namespaced.