Different way of creating UIViews in Swift

xib, storyboard and programatically creating views

You can create custom view using one of the way mentioned in below.

To know more about the code and flow please download the sample project from GitHub here

Click here: To know pros and cons of storyboard, xib and programatic way of creating UIView

Storyboard

Storyboard represent the visual flow of app. It’s good to use when single person is working on the project. If you are working in a team, then using storyboard is difficult.

XIB

How can you create XIB for ViewController?

How can you create XIB for View?

2. To create xib FileNewFileView Add your view name Select target Create the xib under your project directory.

3. Change the size of View in xib to Freeform under attribute inspector tab

For your easy of use try to use same name for both view XIB and class.

How can you load custom XIB of ViewController/View to another ViewController/View?

3. add method for loading XIB in custom class and call that method in default init or in custom init method

4. Finally Create a view object and add it to the parent ViewController/View where you need to load the custom view. Now your project look like this

1. CustomView with default init method and 2. CustomView with custom init method
1. Loading custom view in parent view controller with 3 different way 2. result
Loading Custom ViewController’s XIB

To demonstrate , I’m using main bundle you can use your own bundle name using

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

let nibName = String(describing: type(of: self))

let nib = UINib(nibName: nibName, bundle: bundleName)

let view = nib.instantiate(withOwner: nil, options: nil).first as! UIView

Code for loading xib

The above approach for setting up the custom class for view works fine but there are few drawbacks as well.

To know more about the code and flow please download the sample project from GitHub here

what are the drawbacks of using above approach?

Lets discuss why this crash happen with an example

How can you solve above problem and load view’s xib in another xib/storyboard?

For creating a custom view follow the same process which we discussed in How can you create XIB for View? section. After creating a view class and view’s xib, lets follow the steps

1. Added class name to file’s owner 2. create outlet for view’s subview and view

2. Create and Connect the top level view to File’s owner

3. Design the custom view as per your requirement and add outlet for the view’s class

4. Add code to load xib in the initialisers. You can embed custom view in other xib/storyboard or you can initialise programatically.

Code for loading xib
1. embed custom view in parent view controller, 2. result

In this approach you can call custom view in parent view as same as above approach and even you can embed the custom view in parent view.

AddingFile’s owner’s custom class defines temporarily relationship to help you to create outlets in interface builder. It does not define runtime relationship between class and xib. Creating an instance of class will not load elements from the xib and loading the xib will not create an instance of the class.

what is advantage of XIB?

what is disadvantage of XIB?

To know more about the code and flow please download the sample project from GitHub here

Creating Custom Views programatically

1. Creating view programatically, 2. Result.

My suggestion is try to create view programatically. It’s easier for managing constraints and changing logic as well. Finally you’re the one need to decide which way is suitable for your project.

Creating custom view includes creating any type of view’s subclass that means creating custom button, textfield, textview etc.

To know more about the code and flow please download the sample project from GitHub here

I hope you find this blog helpful. If you enjoyed it, feel free to hit the clap button below 👏 to help others find it! and follow me on Medium. Thanks for reading. ❤❤❤❤

--

--

iOS Developer in walmart

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store