How to add Navigation bar to view controller with tab bar item - ios

I have an app that uses bottom tabs aswell as a side menu, to have the button that initiates the side menu i use the typical three line menu button, to put that there I have a Navigation Bar. With the bar in place there is no way I can get the bar to be on top of the screen. I built it with interface builder, and heres a screenshot. The question is how do i have the navigation bar alone without the other grey bar above it?

The issue you're encountering is due to the fact that you're manually creating a navigation bar for your view controller, instead of using the bar that you get for free by embedding the view controller in a tab bar controller, hence the reason you see two bars. The other answer suggesting hiding the auto-generated navigation bar is not the correct solution. Instead, you should place your menu button and view title in the auto-generated bar instead of manually creating your own (you almost never want to do that, in-fact).
So what you should do instead is set the title property of your view controller to be "News", and the leftBarButtonItem property of the view controller to be your hamburger menu button (an instance of UIBarButtonItem initialized with an image for the icon).
For example (inside your view controller's viewDidLoad method or wherever appropriate):
self.title = #"News";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menuIcon"] style:UIBarButtonItemStylePlain target:self action:#selector(showSideMenu)];

If you want to remove the topmost navigation bar you need use self.navigationController.navigationBarHidden = YES; for view controllers that used for tabs in UITabBarController:
// StoriesViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}

Related

UIView overlapping Navigation Bar

In a viewController I programmatically create a UIView that has the same height of the screen. The problem is that navigation bar is still visible and clickable, but I want it to go under the new view. How can I do that?
EDIT: this is a screenshot of what I have now
Not sure if this is what you actually want, since hiding it is a quite acceptable thing to do. However you can hide the rightButtonItem and disable the left one:
self.navigationItem.rightBarButtonItem = nil;
self.navigationItem.backBarButtonItem.enabled = NO;
And to get back your right bar button, if you need it again somewhere:
self.navigationItem.rightBarButtonItem = self.*whatever*ButtonItem;
See if that works. I'm away from my Mac at the moment, so can't check it myself.
Right now you have taken navigation controller as a root view
controller (Maybe),In this case navigation controller overlaps the
UIVewController's view that's why it comes on the view so you need to
hide the Navigation controller.
What about making it hidden?
self.navigationController.navigationBarHidden = YES;

alternating between toolbar / tab bar

my app is structured as follow: UITabBarController > UINavigationController > ViewControllerOne > ViewControllerTwo.
the UINavigationBar has at the bottom the tab bar, now when the user navigates into the second view controller, i want to be able to hide the tab bar and replace is with a tool bar. i tried this code:
[self.navigationController.tabBarController.tabBar setHidden:YES];
[self.navigationController.toolbar setHidden:NO];
when i run the app the tab bar is hidden but the toolbar doesn't appear. plus, since the last VC is a table view controller, when i scroll through the cells there is a white gap between the table and the bottom of the view. how can i fix that?
That won't work because when you hide the tab bar like that the subviews won't be adjusted properly (that's why you get the white space). You'll have to use
self.hidesBottomBarWhenPushed = YES;
In your init method or awakeFromNib... and then
[self.navigationController setToolbarHidden:NO animated:YES];
In the viewDidLoad for example.
That way the tab bar controller's view is going to layout correctly it's subviews when you hide the tab bar. Just remember to call self.hidesBottomBarWhenPushed = NO; in your first view controller otherwise the tab bar is still going to be hidden when the second view controller is popped from the navigation stack.
Try to assigning toolbar with appropriate frame and adding it to self.tabBarController.view

iOS Navigation Bar: change left bar button from menu to back

I have an app that uses a side menu and has a few main screens that can be accessed from the menu and others that can only be accessed from these screens.
What I want is to have a menu button on the navigation bar that opens the menu and can only be visible on the main screens. On the other screens I want to have a back button, instead of the menu button.
I've already put the menu button like this:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
But I can't figure out how to change it with the back button when I need it.
Assuming than by "main screens" you mean root (first) view controllers in the navigation view controllers corresponding to the selected side menu items, this might be a solution for your problem: you can create a superclass for all your view controllers, say MyBaseViewController and rewrite viewWillAppear: method, that will determine whether it should have a default back button or a "revealSideMenu" button, based on whether it's a "main screen" or not.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self == [self.navigationController.viewControllers firstObject]) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
}
}
you can do that in 2 different ways:
the first is in the viewDidAppear check if the back button is already present (when you push a VC the back button automatically added but is avalaible programmatically only after the viewDidAppear) and then decide to add or not the menu button,
the second is add a parameter to your VC init method like isRoot or hasMenu or whatever you like to name it, and using that flag decide to add the menu or the back button
if you chose to add your own back button you have to call this method in your back selector
[self.navigationController popViewControllerAnimated:YES];
The easiest way, IMO, is just to click on the title bar of the first ViewController and in the Attribute Inspector (⌥+⌘+4) change the Navigation Item info the way you want: Title -> what will show up in the back button* or if you want it to say something other than the Title of the first ViewController or the word "Back" you can just put it in the Back Button field.
*If the Title of the second ViewController is too long for it to fit it will be replaced with the word "Back" (and if the word "Back" doesn't fit it will only have the arrow sign).

How would I implement nested Tab Bar Controllers in an IOS project

I"m trying to implement something like in the wireframe I basically want a Tab Nav Controller on the bottom, and then within on of the views, have another tab like controller. I'm just a little confused as to where I would start with this.
I very much doubt there's a standard user control for that top tab-like control that you want. You may have to construct your own controller and view for that, then manually manage the center view when you get taps on your "View 1" and "View 2" labels.
you should use not Tab Bar Controller, but Navigation Controller toolbar property. It looks like tab bar items but there's no need in implementation another controller instance.
Look at the UINavigationController reference page
A navigation controller object manages an optional toolbar in its view
hierarchy. When displayed, this toolbar obtains its current set of
items from the toolbarItems property of the active view controller.
When the active view controller changes, the navigation controller
updates the toolbar items to match the new view controller, animating
the new items into position when appropriate.
This is some UIViewController class:
- (void)createToolbarItems
{
UIImage *background = [[UIImage imageNamed:#"navbar"] resizableImageWithCapInsets:UIEdgeInsetsMake(2, 2, 2, 2)];
[self.navigationController.toolbar setBackgroundImage:background forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
self.navigationController.toolbar.delegate = self; // optional
//... create some UIBarButtonItem items
self.toolbarItems = #[item1, space, item2, space, item3, space, item4]; // we set items not to navigation controller instance, but the current controller
}
- (void)showToolbarAnimated:(BOOL)animated
{
if(!self.toolbar)
return;
[self.navigationController setToolbarHidden:YES animated:animated];
}

ios force back button style w/ no navigation controller

I'm not using a navigation controller so I'm just getting a plain rounded rect. I want to get the arrow-esque back button instead, is that possible?
From your comments, you would like to use a navigation controller but don't have room for the navigation bar on your root view controller.
self.navigationController.navigationBarHidden
Will hide or show the navigation bar within a view controller. That should make your life simpler.
It is rather simple, create your custom back button like so:
UIButton *button = /* your custom button, most likely with an image */
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];

Resources