Mani
Mani

Reputation: 1320

How to create the UIView sub class with xib?

I want to create UIView commonly. And use that view in all the view controllers. How can create the UIView sub class with xib and use in all the view controllers? Thanks in advance.

Upvotes: 5

Views: 5291

Answers (2)

Bartu
Bartu

Reputation: 2210

  1. You need to create your subclass: File -> New -> File -> Cocoa Touch -> Objective C-class -> Subclass of Target Class Type (UIView on your case)
  2. Then, you create the xib for it: File > New > User Interface > View (name your xib)
  3. Now go and design your xib on the Interface Builder.
  4. Last thing you should do is to change your view's Class Type in Identity Inspector. Your custom xib will be linked with your custom class.

Let me know if you need further detail.

Upvotes: 7

Stavash
Stavash

Reputation: 14304

A UIView subclass first of all must consist of some code defining it, regardless of how you're going to use this view. After you create this class, in order to use it you only need to define the class name in the Interface Builder. Select the UIView you added and change it's class name in the object inspector.

enter image description here

Keep in mind that new properties or functionality that you are adding to this UIView will not be accessible by editing the xib, but only by code.

Upvotes: 1

Related Questions