ios - What is the purpose of using super.init creating custom view with xib?



I want to understand why we use super.init all the time.

For example,

class HeaderStyle1: UIView {

var subview: UIView! //NİB

override init(frame: CGRect) {
    super.init(frame: frame)


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)


func commonInit(){
    subview = Bundle.main.loadNibNamed("HeaderStyle1", owner: self, options: nil)?.first as! UIView
    subview.frame = bounds
    subview.autoresizingMask = [.flexibleHeight, .flexibleWidth]


First: When I instantiate

header = HeaderStyle1(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 80))

super.init(frame: frame) instantiates UIView. Why we need this?

class ViewController: UIViewController {

   var header: HeaderStyle1!

    override func viewDidLoad() {

        createView()     }

    func createView() {

        //Create Header
        header = HeaderStyle1(frame: CGRect(x: 0, y: 0, width:


Second: I add header to UIViewController view as subview. Here header is an instance of HeaderStyle1. So I actually add a class to UIViewController view. How does UIViewController view show instance as view? Is it because of super.init or something? Thank you


1 Answer: 

First , as every class is a subclass of another You call super.init is done to insure that if you call some Instance Variable / Method of the super class, you'll be able to do so as the super class is successfully initiated

enter image description here

suppose that i sublassed NSArray and in some point in app i want to call

  NSArray*er = [myArr mutableCopy];

the copy process will fail if [super init]; failed when i subclassed NSArray as mutableCopy is a method in NSObject that i ignored it's super init

Second , you actually adding an instance of a UIView (HeaderStyle1) to the current view controller's view (which also an instance of UIView) not class to class , this is a hierarchy made by Apple so developers can add different views to their apps to satisfy certain layout needs and modularize their Design so on for all UIKit components (UILabel,UITextfeild.....) that are pre-designed in IOS frameworks to display common components that make sense in small Mobile devices