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:
Why does the Navigation Bar have to be embedded inside the UITableView when using a UITableViewController? (I have tried putting it elsewhere but IB won't let me)
I know that UITableView is a subclass of UIView, but is it OK that the top level element in the hierarchy is not a View (but a TableView)?
Thanks.
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.
There are two ways to use a UINavigationBar in iOS:
Embedded inside a UINavigationController (recommended)
As a standalone object
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.
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.
Related
In my app, I have to show same NavigationBar for all UIViewControllers. This NavigationBar has three buttons and these three buttons which will be act as TabBar functionality, that is each tab has its own stack cycle. I have created custom view for NavigationBar with three buttons, but after adding this custom view to HomeViewController, I have to manually add this custom view for all other view controllers. I don't want to do this.
Is there any simple method to achieve this?
There are a couple of ideas that come to mind. First of all, you could use view controller containment and actually have 1 controller that implements your custom nav bar, and then swap out the contained controller as necessary.
If that's not feasible, you can simply use inheritance and have all your custom controllers inherit from a controller that has the nav bar in place.
Another option could be to write your own UINavigationController subclass. I'm not certain if you can override the UNavigationItem behavior, but if you can, you can just just do that -- instead of the UINavigationController taking its child's UINavigationItem to update its own UINavigationBar, the UINavigationBar perhaps just stays the same, like you're expecting/hoping.
I want to connect UITabbar items to different view controllers. I dont want to use UITabBarController. I have a UITabBar in a ViewController. I am aware of the delegate methods also. Just wanted to know if there is a way to do this through the interface builder(Ctrl+Drag way). Ctrl+Drag only works for elements in UITabBarController and not for UITabbar.
PS: Same question has been asked before but re-posting because there is no satisfactory answer for this.
How do I connect UITabBar items from a UITabBar (not a UITabBarController) to different views in the IB?
I am really sorry. But the fact is that you can't.
UITabBar is a subclass of UIView. Better use UITabBarController . It will provide you a base controller class and works as a container for rest of your controllers.
I have a SegmentedControl at the bottom of my EventsMasterViewController, but I also need it to be on the EventsDetailViewController.
The MasterViewController has a TableView that I use to list events, and the SegmentedControl acts as a "filter". However, clicking on an event needs to bring up the Event Detail View, but I need to keep the SegmentedControl at the bottom for navigation.
What's the best approach for this?
UISegmentedControl is not meant to be used for "navigation", filtering makes sense but why do you want to show these "filters" on the detail view where there is nothing to filter?
If you need navigation use UITabBarController...
If you need "filtering" use UISegmentedControl and add it to the bottom toolbar of your UINavigationController.
You will need to create one UISegmentedControl for each UIViewController but again mind you that "filters" on detail view make no sense.
UPDATE
In my opinion you should implement UITabBarController as the root controller to be used for your navigation. Then use UINavigationController + UIViewController for your master/detail views. And use one UISegmentedControl in your master controller to modify the table view output.
it depends on what method you bring up the Detail View.if it's present,then it's impossible because it will always present from the bottom.if it's navigation controller,you can set the frame of the detail view smaller to let the segment can be seen.pay attention to the boundary.
I am completely new to ios development and I am only interested in developing for ios5.
I have an app with some scenes, they are mostly tableviews. I also use a navigation controller
I however need to add some status text and buttons that are always visible in all scenes and thought that a toolbar added to the navigation controller should do the trick.
so i thought that i should only have to drag out a toolbar in storyboard to the navigation controller, but it does not stick there. I can add it to the bar underneath with first responder and navigation controller but that does not help me (small icons).
I can also not add it to my table view (but if i drag out a plain view I can add it there)
do I have to make my own custom navigation class that the navigate view uses and then programatically add my toolbar?
Had the same question recently. Check Attributes Inspector in your ViewController's properties in storyboard. There you can define a Bottom Bar.
Well, inside the UINavigationController, you should have something... A UIViewController for instance. You can easily add a UIToolBar by dragging the object inside the UIView of the UIViewController. What might being happening is that as the root view you have the UITableView, in that case I think you can't do that. But to better understand, just take a small print screen of your StoryBoard.
If you zoom up to 100% on the storyboard it should drag.
I am new to iOS development and have been fiddling with this for a while and can't seem to find an answer or get it to work.
I have a navigation controller as the root view controller of my app. There is a table and selecting a cell will open a view that I want to be scrollable with page control.
I make a new subclass of UIViewController, call it PagingViewController. In interface builder, I just drag in a UIScrollView and a UIPageControl and lay it out appropriately on the screen.
When I push the PagingViewController onto the navigation controller, the UIPageControl does not display.
When I modify AppDelegate.m to use PagingViewController as the root view controller and start the app, the UIPageControl does display.
I'm trying to figure out why UIPageControl does not display. I think it has something to do with being used in conjunction with the navigation controller, but other answers and posts I've read seem to indicate that it is indeed possible.
And yes, in both cases, I initiate the PagingViewController in the same way. I am confused. Any help would be appreciated. Thanks.