Is there a way to subclass UIToolbar? - ios

I ask this as I would like a global toolbar in my app (similar to the Facebook app with buttons on it). How do I subclass it so that I can add it on all of my ViewControllers? I've created a new class but UIToolbar isn't in the options. How would I modify NSObject manually?
Edit:
My controller hierarchy is as follows:
A login page, which pushes (flip horizontal) -----> Tab Bar Controller. How would I add this toolbar to the top of each page in the tab bar controller?

If you are using a UINavigationController as your window's root view controller, you can let the navigation controller display a toolbar. You need to tell it to show the toolbar by sending it a setToolbarHidden:animated: message. If you want the toolbar to have the same buttons (or other subviews) in all your view controllers, make sure you set each view controller's toolbarItems property to the same array of items.
If you are not using a UINavigationController as your window's root view controller, edit your question and describe your view controller hierarchy.

Related

Why does embedding a View Controller in a Navigation Controller, then in a Tab Bar Controller attach the nav bar to the Tab Bar Controller?

Back when I first created the foundational layout for the app I'm currently working on in Storyboard, I did it in two steps:
Selected my View Controller and used Editor->Embed In->Navigation Controller.
Selected my View Controller again and used Editor->Embed In->Tab Bar Controller.
This was the resulting layout:
Question 1: Why do these steps create such an odd layout?
In this weird layout that seems to imply that the Navigation Controller is attached to the Tab Bar Controller as its parent, the only way I can get navigation items to display in the app (in the view controlled by the View Controller on the right) is by placing them in the nav bar of the Tab Bar Controller scene.
However, this creates various issues, including not being able to create an IBOutlet in my View Controller file for a custom view I drag into the title view slot in the Tab Bar Controller scene. Meanwhile, dragging anything into the navigation bar in the View Controller scene just makes it not appear in the app when it runs.
Question 2: How can I fix this layout so that I can control-drag from navigation items into my View Controller file? Or is everything actually correct, and I'm just trying to force something I shouldn't? (My intention here is to be able to set the custom title view's text in my View Controller code.)
Its obvious, if you want to embed MyViewController to NavigationController then you need to change your second step and need to embed NavigationController to Tab Bar Controller.
Selected MyViewController and used Editor->Embed In->Navigation Controller.
Selected NavigationController Embed with MyViewController and used Editor->Embed In->Tab Bar Controller.
It should look like this.
Note: You need to embed MyViewController to navigationController only if you want to perform push operation on this TabBarItem means with MyViewController other wise simply embed MyViewController to TabBarController no need to embed it with NavigationController.

Segue-ing to Embedded view controller

I have have a view controller that is embedded in a UINavigationController and I want to segue to another view controller that is also embedded in a different UINavigationController. If I try to use a push segue, I get an error saying that I can't push a UINavigationController. However, I don't think using a modal segue is appropriate. How should I go about this?
How should I go about this?
You should use a single navigation controller.
the problem is that I want different bar button items for each view controller and they won't change
Each view controller can set up the bar buttons however it likes. Take a look at UINavigationItem. Each view controller has a navigation item, and the navigation item has various properties such as leftBarButtonItems and rightBarButtonItems that you can use to set the buttons.

Custom navigation controller to show fix top menu

I want to create a fixed top menu with buttons to navigate through views, right now i have a navigation controller but I don't know how I can present a view controller from the navigation controller and I don't want the back functionality that included in the navigation controller. Is there an easier way were I can use a view controller as a menu and have it always presented static on top, even if the views underneath is changed?
What kind of controller would be easiest controller to use, to create a fixed top menu?
This is the basic setup.
The top bar is a UIToolbar with four barbutonItems
You could imbed the child view controller in the container view and switch the child view controllers when user tap the buttons.
It sounds like you want to create your own parent view controller. I suggest you create a custom UIViewController that has a container view on it, and that you load child view controllers into the container view based on the user's interaction with your navigation menu. This is pretty straightforward to do.
Your view controller will probably act more like a tab bar controller than a navigation controller if I understand what you're looking for.

TUITabBarController inside UINavigationController problems

I want to add UITabBarController as root view of my UINavigation controller. The purpose of that is that I want to make similar navigation as Facebook app: Then user select contact on Contacts tab - pushed view with user details doesn't have TabBar.
So, I have navigation controller, tab bar as a root view or it and couple TableViewControllers inside this UITabBarController.
I have two major issues with that:
I can't assign title for navigation bar for root views of tabbar. If I open contact details I have navigation bar title and no tab bar (as I wanted). But if I select Contacts or Favorites - those views have empty navigation bar title. I've tried to set it on viewDidLoad and viewDidApper without any success
Even worse issue. When I press Favorites table view insets are broken. first cell is under navigation bar. First loaded tableview (in my case Contact) displayed correctly. Adjust scroll view insets is YES for both TableViews.
Update:
Solution storyboard (note property "Hide Bottom Bar on the Push" is switched to YES).
I was also struggling with UITabBarController + UINavigationController issue. When UITabBarController was contained by UINavigationController, content insets were acting really weird.
I would suggest to use navigation controllers inside UITabBarController and when next controller is pushed, set 'hidesBottomBarWhenPushed' on that controller to YES.
This would also solve setting navigation bar title, as navigation controller is closer to view controller. You can just use self.navigationItem.
If you leave it to current setup - UITabController contained within UINavigationController, then you need to modify navigationItem on controllers tabBarController (self.tabBarController.navigationItem). Note that best place for it would be in view controllers 'viewWillAppear' for every controller contained within UITabBarController, as they share one navigation item.

Using UINavigationController view

I would like to better understand the use of UINavigationController's.
I have setup a new project with a UINavigationViewController, as well as two other view controllers.
In my app delegate I have the following:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
firstViewController = [[NCTFirstViewController alloc] initWithNibName:#"NCTFirstViewController" bundle:nil];
navController = [[NCTNavViewController alloc] initWithRootViewController:firstViewController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
Within my UINavigationController.m file I can set the title and set up buttons, but this does not work.
This only works if I setup the self.navigtionController items in the actual View Controller itself. Is this correct, or is there something I should be doing in the UINavigationController to get this to work.
Ideally I am looking for a UINavigationController that handles all the pushing to other controllers. In the long run it would be used as a menu system. So if the user clicks a button at the top, they are pushed to a new View Controller but without the back option, simply the same menu items in the navigation bar at the top, which shows a new center view controller.
The issue I am having is understanding how this is setup. If I have to setup this in the view controllers itself, would they all not duplicate the same code. Of course I could setup all the 'movement' in the AppDelegate, but this doesn't seem correct and would pack up the App Delegate itself.
If you look at the UINavigationController class reference, it says (emphasis added by me, but you should read it all so that you understand how this works):
Updating the Navigation Bar
When the user changes the top-level view
controller, whether by pushing or popping a view controller or
changing the contents of the navigation stack directly, the navigation
controller updates the navigation bar accordingly. Specifically, the
navigation controller updates the bar button items displayed in each
of the three navigation bar positions: left, middle, and right. Bar
button items are instances of the UIBarButtonItem class. You can
create items with custom content or create standard system items
depending on your needs. For more information about how to create bar
button items, see UIBarButtonItem Class Reference.
The bar button item on the left side of the navigation bar allows for
navigation back to the previous view controller on the navigation
stack. The navigation controller updates the left side of the
navigation bar as follows:
If the new top-level view controller has a custom left bar button
item, that item is displayed. To specify a custom left bar button
item, set the leftBarButtonItem property of the view controller’s
navigation item.
If the top-level view controller does not have a
custom left bar button item, but the navigation item of the previous
view controller has a valid item in its backBarButtonItem property,
the navigation bar displays that item.
If a custom bar button item is
not specified by either of the view controllers, a default back button
is used and its title is set to the value of the title property of the
previous view controller—that is, the view controller one level down
on the stack. (If there is only one view controller on the navigation
stack, no back button is displayed.)
The navigation controller updates
the middle of the navigation bar as follows:
If the new top-level view controller has a custom title view, the
navigation bar displays that view in place of the default title view.
To specify a custom title view, set the titleView property of the view
controller’s navigation item.
If no custom title view is set, the
navigation bar displays a label containing the view controller’s
default title. The string for this label is usually obtained from the
title property of the view controller itself. If you want to display a
different title than the one associated with the view controller, set
the title property of the view controller’s navigation item instead.
The navigation controller updates the right side of the navigation bar
as follows:
If the new top-level view controller has a custom right bar button
item, that item is displayed. To specify a custom right bar button
item, set the rightBarButtonItem property of the view controller’s
navigation item.
If no custom right bar button item is specified, the
navigation bar displays nothing on the right side of the bar.
The
navigation controller updates the navigation bar each time the top
view controller changes. Thus, these changes occur each time a view
controller is pushed onto the stack or popped from it. When you
animate a push or pop operation, the navigation controller similarly
animates the change in navigation bar content.
So, to do what you want, you do need to set the title and buttons in each view controller. The easiest way to set the title is to set it in the storyboard/xib, or in code when you create it.
You should also replace the root view controller instead of pushing the view controller onto the navigation stack so that you don't keep adding view controllers to the stack. This also avoids displaying the back button, and you won't need to explicitly get rid of it. You would do this by using the setViewControllers:animated: method like this:
[self.navigationController setViewControllers:[NSArray arrayWithObject:theNewViewController] animated:YES];
There is only one NavigationController and this NavigationController controls all the ViewControllers. So barButtonItems and titles are set in each ViewController, you should not set them directly to a NavigationController.
If you want to push a new ViewController and won't push back, I think this may cause some problem because the NavigationController is using a Stack to handle all the ViewControllers. Seems that you will push a ViewController in the stack but not pop it, and maybe you will get some behavior you don't want.
If you still want to implement this, I think you cannot avoid do similar setting in different ViewControllers.

Resources