Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load custom UIView from xib programmatically

Tags:

ios

swift

uiview

I have created a custom UIView in MySample.xib. I have added the class MyView to the File Owner of xib.

MyView.swift

class MyView: UIView {

    @IBOutlet var view: UIView!

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

        setup()
    }

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

        setup()
    }

    func setup() {
        NSBundle.mainBundle().loadNibNamed("MySample", owner: self, options: nil)            
        self.addSubview(self.view)
    }
}

I am now loading this MyView from MyController file like this:

MyController.swift

class MyController: UIViewController {
    init() {
        super.init(nibName: nil, bundle: nil)

        view.addSubview(MyView())

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

Now to display this view, I am using to following code from another controller's UIButton:

presentViewController(MyController(), animated: true, completion: nil)

This does display the view on screen. But the problem is, it doesn't accept any user interaction. In my custom view, I have a UITableView which does display the data but it doesn't scroll or get tapped due to lack of user interaction.

Any idea what I am doing wrong?

like image 370
codelearner Avatar asked Apr 10 '16 23:04

codelearner


People also ask

How do I load a custom view in storyboard?

Using a custom view in storyboardsOpen up your story board and drag a View (colored orange below for visibility) from the Object Library into your view controller. Set the view's custom class to your custom view's class. Create an outlet for the custom view in your view controller.


2 Answers

There are some unnecessary things in your example.

I am still not sure what are you trying to do, but if you want to add a custom view from xib to your view controller then:

  1. Create a view in a xib file , you don't need to override init , and you can't init view from xib using the default init UIView() , so please remove init method from your MyView class.

  2. In your xib make sure that your view that you see in the IB is of the class type you want to use (i guess MyView class).

  3. In your view controller init the view like this:

    class MyController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Get all views in the xib
            let allViewsInXibArray = NSBundle.mainBundle().loadNibNamed("MySample", owner: self, options: nil)
    
            //If you only have one view in the xib and you set it's class to MyView class
            let myView = allViewsInXibArray.first as! MyView
    
            //Set wanted position and size (frame)
            myView.frame = self.view.bounds
    
            //Add the view
            self.view.addSubview(myView)
    
            //TODO: set wanted constraints.
        }
    }
    
like image 115
Oleg Sherman Avatar answered Sep 21 '22 04:09

Oleg Sherman


You don't have to re-instantiate this twice already if you using the design pattern.

It's so simple. Just write:

class MyController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    //Get all views in the xib
    let view = MyView() 
    self.view.addSubview(myView)

    //TODO: set wanted constraints.
}}

And It will work.

like image 29
Mohamed Deux Avatar answered Sep 21 '22 04:09

Mohamed Deux