Custom View by use of xib in swift

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

Following these are simple steps for creating the custom view
1: First create a 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 a custom class (It must be same as the 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 all the above steps for creating the custom UIView : –

1: First create a single view application project.

first, open your Xcode and create the new single view Application project with the 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

Java Operators Java Operators An operator is used to perform an operation over one or more operands. It is a character that represents an action, for example * is a...
Loop Control Statement Loop control statements are used when a section of code may either be executed a fixed number of times. C gives you a choice of three types of loop st...
Use of CollectionViewDataSource method moveItemAt ... Use of CollectionViewDataSource method moveItemAt for Reordering CollectionView cell In IOS (Swift 4) In this tutorial we are study about how we can ...
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...
Firebase Push notification | Swift tutorial Push Notification by firebase in IOS.For Firebase Push notification we need to have paid apple developer account. You need to Enroll into the Apple D...
Asset Color Catalog | Swift Tutorial Asset Color Catalog is the feature of XCode 9. BY the use of this feature we can maintain the colors easily and avoid the mistakes. We can create the ...
Author

Write A Comment