user5154504
user5154504

Reputation:

How to define a class and module for a view created programmatically in swift

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

Upvotes: 3

Views: 2861

Answers (1)

shallowThought
shallowThought

Reputation: 19602

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:

Upvotes: 3

Related Questions