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

Invitation Letter for Visa Invitation Letter Samples These letter are necessary for taking visa of another country. so these samples are the most important document. Sample 1 ...
Continue Statement in JAVA Continue Statement in JAVA Suppose you are working with loops. Sometime it is desirable terminate the loop or skip some statement inside the loop wit...
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...
Decision Control Statement C has three major decision making instructions— the if statement, the if-else statement, and the switch statement. The if Statement It is used to ex...
Drag and Drop Table View cell in IOS 11 & swi... Drag and Drop Table View cell in IOS 11 & swift 4
Loops in java Loops in JAVA Loops are used to execute a set of statements repeatedly until a particular condition is satisfied. Loop is used in programming ...

Leave a Reply

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