fandro
fandro

Reputation: 4903

Swift can't fixed navigation bar for UITableView

I am trying to display a fix navigation bar for my UiTableViewController, I have a first ViewController and when I click on it, this will open my UITableViewController Here is the code of the click :

let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
        vc.myObject = object // I pass some data
        presentViewController(vc, animated: true, completion: nil) 

the UItableView is correctly display but not navigation bar appear, if I add one, the navigation bar scroll with the table view and I don't want this behavior.

I tried this without success :

Go to the Editor menu, and click on the Embed In submenu, and choose Navigation Controller

And tried to change some settings here :

enter image description here

Upvotes: 0

Views: 874

Answers (2)

Muhammad Ali
Muhammad Ali

Reputation: 2183

Actually, in your case you want to show navigation and for navigation you have to push your view controller to a UINavigationController thats why the solution is :

let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)

presentViewController offers a mechanism to display a modal view controller; i.e., a view controller that will take full control of your UI by being superimposed on top of a parent controller & establish a parent child relation b/w presenting & presented view controllers.

where as

pushViewController offers a much more flexible navigation process where you can push & pop a new controller to UINavigationController, so to go back to the previous one, in a ordered way. Imagine that controllers in a navigation controller will just build a sequence from left to right like building a stack of view controllers stacking upon each other.

Upvotes: 3

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

Do it this way:

let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)

Upvotes: 2

Related Questions