Custom View by use of xib in swift

Custom View by use of xib in swift

We can create the custom and common view by the use xib in swift 3.1 and xcode 8.3.2 . Custom views are common and its very good technique for create the effective User interface. There are two ways to custom layout views: building it in interface builder or by coding. But interface builder way is more comfortable. If you layout a view in the interface builder, then it is save your time. There we are using the ‘@IBDesignable’ also by which we rander the view directly in storyboard and XIB ViewControllers .

Following these are simple steps for creating the custom view
1: First create the single view application project.
2: Create the .xib file (like CustomUIView.xib)
3: Design your customView user interface in interface xcode (use the Auto Layout constraints).
4: Create a custom file UIView extended file (like CustomUIView.swift).
5: Set .xib File owner to custom class (It must be same as class name ).
6: Implement both CustomView initializers: init(coder:) and init(frame:).
7: Load the UIView from the .xib file using the NSBundle and UINib classes.
8: Now use your custom UIView.

Now we explain the all the above steps for creating the custom UIView : –

1: First create the single view application project.

first open your xcode and create the new single view Application project with name (example: AGCustomViewBy_XIB)



2: Create the .xib file (like CustomUIView.xib)

Now you want to create the new user interface like .xib file. Right click of your project folder and select the “New File…”, Now xcode open the a view for which type of file you want to create. Chose the View under the user interface menu like


3: Design your customView user interface in interface xcode (use the Auto Layout constraints).

Now go to your CustomUIView.xib file for create the user interface for our custom view. First you go right side menu in .xib file and select the Show the attribute inspector and change the size inferred to Freeform.

 

 

Now you start to create the user interface for our custom view , you go to right bottom corner in .xib file and select the “show the object library” menu and search the label and ImageView after that you drag and drop these things on .xib View and set the auto layout constraints. An please change the background color of view for increase the visiblity of view.



 

4: Create a custom file UIView extended file (like CustomUIView.swift)

Create the custom file UIView Same as .xib file but you want to select the option in source “Cocoa Touch Class” and create the new file with name CustomUIView.swift which have UIView subclass.

 

 



 

5: Set .xib File owner to custom class (It must be same as class name )

We are near to our destination, For connect the .swift and .xib file we want to set owner of .xib file to CustomUIView.swift and connect the IBOutlet of our view


 



 

6: Implement both CustomView initializers: init(coder:) and init(frame:)

Please add these methods to your customView class these are overrided methods of UIView init(coder: ) and init(fram:)

 

override func draw(_ rect: CGRect) {

        // Drawing code

    }    

    override init(frame: CGRect) {

        // Call super init

        super.init(frame: frame)

        // 3. Setup view from .xib file

        configureXIB()

    }

    required init?(coder aDecoder: NSCoder) {

        // 1. setup any properties here

        // 2. call super.init(coder:)

        super.init(coder: aDecoder)

        // 3. Setup view from .xib file

        configureXIB()

    }

 

7: Load the UIView from the .xib file using the NSBundle and UINib classes.

Its an last step for this custom view creation. We want to register the .xib nib file name into our customView class, For this we implement this methods in class and for creating the flexible custom view we set the .autoresizingMask property for this view.

 

func configureXIB() {

       customView = configureNib()

        // use bounds not frame or it’ll be offset

       customView.frame = bounds

        // Make the flexible view

       customView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]

        // Adding custom subview on top of our view (over any custom drawing > see note below)

        addSubview(customView)

    }

    func configureNib() -> UIView {

        let bundle = Bundle(for: type(of: self))

        let nib = UINib(nibName: “CustomUIView”, bundle: bundle)

        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        return view

    }

 

8: Now use your custom UIView

We’re done! Now we’ve got a custom view made from a xib file that we can use throughout our project. Lets see how it look’s .

 

 

If you want to show the reflection of this custom view in your storyBoard then please use the
@IBDesignable in your class like



For getting the complete project Download here.

Related Post

E-Mail Format Formal Email / Letter Format A formal email is in general sent to someone you don’t know well or to someone who’s in authority. Examples of someone w...
Variables in Java Variables in Java A variable is something which is associated with a value can be changed. For example: int i=10; Here variable name “i” wh...
Drag and Drop Collection View cell in IOS 11 &... Drag and Drop Collection View cell in IOS 11 & swift 4 In this tutorial we are explain how to drag and drop collection view cell in two collectio...
Data Type And Storage And Keywords To store data inside the computer we need to first identify the type of data elements we need in our program. There are several different types of dat...
Getting started with C Getting started with C : If we want to communicate with our computer it is very much needed that our computer understands what we want to say to it. ...
First Java Program First Java Program NOTE: You need to have java installed on your system. Step 1: Open a text editor, like Notepad on windows and TextEdit on Mac. W...

Leave a Reply

Your email address will not be published. Required fields are marked *