Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 524
Mani Avatar asked Apr 05 '13 14:04

Mani


People also ask

How do I create an XIB file?

Create a XIB FileRight click on the portion of the screen where your project's files are (view controller, storyboard, etc), and choose new file . Xcode will prompt you for which file type you'd like to create. Choose the View option under the user interface menu.

How do I add XIB in Objective C?

Create new “View” file from the User Interface menu. This will be your xib that represents a person in your app (let's say it's a dating app). Call it “PersonXIB” (or whatever you want). You're going to see what looks very much like a single view storyboard file with a blank view.


2 Answers

  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.

like image 61
Bartu Avatar answered Sep 29 '22 07:09

Bartu


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.

like image 25
Stavash Avatar answered Sep 29 '22 07:09

Stavash