user9137935
user9137935

Reputation:

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()  
    }
}

Upvotes: 3

Views: 4696

Answers (2)

Jaydeep Vyas
Jaydeep Vyas

Reputation: 4470

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)
}

Upvotes: 2

Lamour
Lamour

Reputation: 3030

The issue is that you didn't actually get the parent view for your UINib object.

Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)

The line above returns an [Any] in your case you aren't even using the view that it is returning. so the idea is to get the first object from it and cast it as UIView such as:

 Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)?.first as? UIView

Personally this is how I interact with a Nib. I create a view property of type UIView that can be referred as the parent view for the nib, and all subviews get added to it instead of self.

Something like this:

final class SomeNibView: UIView {
    public var view: UIView!


    private func setup() { // called to the initializer 
        // grab the views from loadNibNamed
        guard let _view = Bundle.main.loadNibNamed("name", owner: self, options: nil)?.first as? UIView else { return }
        // set it to our view property 
        view = _view
        // add this property to the nib subview aka self 
        addSubview(view)

        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }

    private func addMulitpleSubviews() {
        // instead of doing self.addSubview(....) when it comes to add other subviews 
       // you'll do this view.addSubview(....)
   }
}

Upvotes: 3

Related Questions