Mujtaba
Mujtaba

Reputation: 100

how to call xib file from storyboard on button click in swift

I am new in swift I want to call .xib file from storyboard on button click. I am using code like this but it crash because .xib file is not in storyboard

 let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
 let newViewController = storyBoard.instantiateViewController(withIdentifier: "abcViewController") as! abcViewController
 self.present(newViewController, animated: true, completion: nil)

this code

 let newViewController = abcViewController(nibName: "abcViewController", bundle: nil)
 self.present(newViewController, animated: true, completion: nil)

screen like this enter image description here

I just change line and now its work fine

 let newViewController = abcViewController(nibName: "abcViewController", bundle: nil)
 //self.present(newViewController, animated: true, completion: nil)

 self.navigationController?.pushViewController(newViewController, animated: true)

Upvotes: 0

Views: 1276

Answers (3)

Wassim Ben Hssen
Wassim Ben Hssen

Reputation: 529

@Mujtaba to fix the screen issue not showing full screen go to you view controller in the storyboard and change the presentation style to full screeen enter image description here

Upvotes: 1

Jaydeep Vora
Jaydeep Vora

Reputation: 6213

let newViewController = abcViewController(nibName: "xibFileName", bundle: nil)
//If you want to present xib
self.present(newViewController, animated: true, completion: nil)

//If you want to push xib in navigation stack
self.navigationController?.pushViewController(newViewController, animated: true)

Upvotes: 1

SKD
SKD

Reputation: 1493

Can try this:

// Register Xib
let abcViewController = ABCViewController(nibName: "abcViewController", bundle: nil)
// Present VC "Modally" style
self.present(abcViewController, animated: true, completion: nil)

set the ID as abcViewController as same class name

Thanks

Upvotes: 0

Related Questions