Reputation: 2310
I have some static cells that I want to display, so I have a UITableViewController. There is also a NavigationBar in this scene that contains some buttons at the top. The setup looks like this:
If I had a UIViewController that contained a UITableView in it, the setup would look like:
So, the question is:
Thanks.
Upvotes: 2
Views: 1557
Reputation: 7845
There are two ways to use a UINavigationBar
in iOS:
UINavigationController
(recommended)For your particular situation, I'd recommend that you put your UITableViewController
as the rootViewController
of a UINavigationController
. That way you automatically get a navigation bar which you can customize according to your needs. In a typical user experience, when you tap some of your table view rows a new view controller will be pushed onto the navigation stack, so you'll probably end up needing a navigation controller anyway.
What if you decide to use a navigation bar as a standalone object? This is perfectly fine, you can use it inside a view hierarchy as an ordinary UIView
, but you'll need to create another object that implements the UINavigationBarDelegate
protocol and set it as the delegate
property of your navigation bar. If you use a UINavigationController
the delegate is already set and configured for you. You also need to add/remove navigation items (instances of UINavigationItem
) to your navigation bar by using the pushNavigationItem:animated:
and popNavigationItemAnimated:
methods.
And about your question on the view hierarchy, you can use a UITableView
anywhere a UIView
is required. The only caveat is that a UITableView
is a view hierarchy on its own and that may restrict your layout a little bit.
Upvotes: 1
Reputation: 8200
You shouldn't be placing your UINavigationBar
in your UITableView
. You should be putting your UITableViewController
in a UINavigationController
, because that will provide a UINavigationBar
for you.
So if you select your UITableViewController
in the storyboard, you can choose Embed In -> Navigation Controller from the Editor menu. This would be the proper way to do it.
Upvotes: 2
Reputation: 4905
The way a UITableViewController works, is its root view is a UITableView. So there is no way to put the UINavigationBar anywhere other than in the UITableView.
I tend never to use a UITableViewController as it doesn't really give you much.
If you particularly want to use the UITableViewController, I don't believe that there is any real problem in having the navigation bar within the table view. You just need to make sure that you set the contentInset on the table view such that the navigation bar doesn't block the content. Though it seems a bit backward to do it this way.
My recommendation would be to just use a normal UIViewController with a navigation bar and a table view.
If you actually need functional navigation, you need to put your UITableViewController within a UINavigationController.
Hope this helps :)
Let me know if anything is still unclear.
Upvotes: 1