I have a few questions regarding nibs.
- How do I load a nib onto my UIView in a View Controller dynamically or programmatically? I know you can just add the custom class of the nib by entering the nib's swift class here:
What I'm looking for is how to change that attribute, for example on a button press I would like to remove it or add it. I tried instantiating the swift file but I can't pull it off. I also tried changing the owner of the .xib file but it only wants a UIView so I can't make my View Controller its owner. I tried using
view.addSubiew(view: UIView)
But I don't know how to instantiate my Nib file properly as a UIView. It keeps asking for an NSCoder and I have no clue how to instantiate one either. It would help if someone could tell me how to add and remove the custom class that would be helpful.
How do I pass data from my View Controller to the nib?
How do I run functions once the nib has fully loaded? Here's the code of the .xib file's swift class.
required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) if (self.subviews.count == 0) { guard let view = loadViewFromNib() else {return} view.frame = self.bounds self.addSubview(view) mainView = view -> btn.layer.cornerRadius = 5 } } private func loadViewFromNib() -> UIView? { let bundle = Bundle(for: type(of: self)) let nib = UINib(nibName: "NibFile", bundle: bundle) return nib.instantiate(withOwner: self, options: nil).first as? UIView }
This loads the view just fine but whenever I try to run anything else in the code blocks, like where it's marked (I wanted to change the corner radius of a button once the view is loaded), I keep getting a nil IBOutlet. It just keeps saying that the button is nil. Where do I add this function?
I also tried doing this:
override func awakeFromNib() {
btnLogin.layer.cornerRadius = 5
}
Still nil, I'm getting confused here and research comes to dead ends or just what I don't need. Any help will be appreciated. Thank you!