Search code examples
iosswiftxib

How to add xib view to view controller swift 3


I am trying to add this XIB view to my viewcontroller:

import UIKit

class TranslateView: UIView {
    @IBOutlet var TranslateView: UIView!

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

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

    private func commonInit(){
        Bundle.main.loadNibNamed("TranslateView", owner: self, options: nil)
        addSubview(TranslateView)
        TranslateView.frame = self.bounds
        TranslateView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    }
}

And I am loading it into my view controller here:

import UIKit
import AVFoundation
import Vision

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
    let captureSession = AVCaptureSession()

    let button: UIButton = {
        let button = UIButton()

        button.addTarget(self, action: #selector(ViewController.pressed(sender:)),for: .touchUpInside)
        button.backgroundColor = UIColor.clear
        button.setTitle("Translate", for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        setupCaptureSession()
        view.addSubview(button)
    }

    @objc func pressed(sender: UIButton!) {
        //add label
        button.removeFromSuperview()

        let headerView = Bundle.main.loadNibNamed("TranslateView", owner:
            self, options: nil)?.first as? TranslateView
        self.view.addSubview(headerView!)
        headerView?.frame = CGRect(x:0, y: 0, width: view.frame.width, height: 50.0)
    }

I have removed most of the code to keep this question short. I am not using a storyboard because I want to use the live cameraview as the background of my app. And by doing it in this way is the simplest. What I want to happen is that when I press the button the entire screen should be replaced with a new view which is TranslateView.xib .

Hope someone can help. Or maybe if there is a much better simpler way then using XIB to achieve the same result let me know to.

Please give a code example in Swift 3, not just an explanation what to do because I already know what I need to do just not how to code it.


Solution

  • I think it is better if you convert your TranslationView to a UIViewController. It won't be a big change but then adding TranslationView to your ViewController will be much simpler. For example:

    class TranslateView: UIViewController {
        override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
            super.init(nibName: "TranslationView", bundle: nil)
        }
    }
    

    Then you can load this controller into your ViewController class like below:

    let headerView = TranslationView()
    self.addChildViewController(headerView)
    self.view.addSubview(headerView.view)
    headerView.didMoveToParentViewController(self)
    headerView.view.frame = CGRect(x:0, y: 0, width: view.frame.width, height: 50.0)