3

Let's say I create a view like this:

let myView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)

How can I give that view a custom class and module like I would in IB?

I am either searching for the wrong thing, or no one has asked this question. I also haven't tried anything because I can't figure out where to even start.

Update:

This is what I mean by adding a class and module like you would in IB:

enter image description here

1
  • The only thing that you need to do is to make another target (a cocoa touch framework in your case) and make the class a member of that target (and of course link the framework) Commented Mar 27, 2017 at 10:54

1 Answer 1

3

Simply instantiate it instead of UIView.

Assuming this is your custom view:

class MyCustomView: UIView {
    //...
}

Here is how to instantiate it:

let myView = MyCustomView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
self.view.addSubview(myView)

The module is the module your source file of class MyCustomView: UIView... is member of. When developing an iOS application (not a framework or other target types) this is your app. You can choose the "Target Membership" in Xcodes Inspector when selecting a source file:

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, but what about the module?
Updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.