Search code examples
objective-cswiftxib

Xib not showing up in view


I have a Xib file trying to set it up with my storyboard. Everything in the Xib file is fine, but for some reason, it's not showing. I imported a file from GitHub which is set to my Xib, it's in Objective-C and I set the bridging, no errors. But when I run it nothing shows its blank. Did I not set something in the View Controller? Everything is done programmatically and I just set the class in storyboard.

Screenshot of storyboard:

enter image description here

What the simulator gives me when I push to the ViewController:

enter image description here

This is what I'm supposed to see:

enter image description here

What I am trying to implement - https://github.com/jberlana/JBCroppableView

My XIB class

import UIKit

class CropViewXIB: UIView {

    @IBOutlet weak var ImageView: JBCroppableImageView!

    @IBAction func SubAction(_ sender: Any) {
        ImageView.removePoint()
    }

    @IBAction func AddAction(_ sender: Any) {
        ImageView.addPoint()
    }

    @IBAction func UndoAction(_ sender: Any) {
        ImageView.reverseCrop()
    }

    @IBAction func CropAction(_ sender: Any) {
        ImageView.crop()
    }


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

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

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

    }
}

my view controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var cropView: CropViewXIB!

    override func viewDidLoad() {
        super.viewDidLoad()  
    }
}

Solution

  • Try to load xib using programming not using storyboard.

    override func viewDidLoad()
     {
        super.viewDidLoad()
        guard let yourXIB = Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)?.first as? CropViewXIB else { return}
    self.view.addSubview(yourXIB)
    }