How do I instantiate a view from a XIB file programmatically and insert it into my main view?

Hi all,


I feel like I must be missing something very simple, but I'm hoping one of y'all can help me here.


I want to define a view which is only displayed under certain conditions in its own XIB file. I've done this, and associated it with its own class to handle its properties and methods.


Here's the stripped down class for the stand-alone view:


class ManageMyProfileView: UIView {
   
    class func instanceFromNib() -> ManageMyProfileView {
        return UINib(nibName: "ManageMyProfileView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! ManageMyProfileView
    }
}


Now, I'm trying to instantiate the view (ManageMyProfile) using something similar to the following in my main ViewController file:


        let mmpfv = ManageMyProfileView()
        mmpfv.frame = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0)
        self.view.addSubview(mmpfv)


I'm getting no errors, but nothing is showing up.


I've perused Stack Overflow, but I'm clearly missing something because I feel like I've tried all the combinations of things to put into the stand-alone view's own controller's and the main viewcontroller's class files, and nothing helps.


I'm working in the latest non-beta version of Xcode, deploying to iOS 10 and 11 targets, running Swift 4.


Thanks in advance for any help you can provide. I know I could solve the problem by sticking a hidden view into my main view controller in the main storyboard, but things are getting messy there, so I'm trying to do the right thing and make the whole shebang manageable.


All my best,


Ben

How do I instantiate a view from a XIB file programmatically and insert it into my main view?
 
 
Q