Hiding (not removing) A UITabBarController UITabBarItem - objective-c

I need to hide a button on the tab bar, but still have it accessible by code as needed. I know I can completely remove the button, but then I cannot access that view anymore.
So in my case, I want my home screen to be visible when the app first loads, but do not want the tab to show up for it. If they navigate away from that screen, I will add a custom "home" button in the navigation bar at the top.
However, if I remove the tab bar item, I no longer go to the home screen anymore but to what was originally the 2nd tab. Is there a way to only hide the tab bar item and still access it in code?
So you can see how I'm accessing the tab bar to begin with, here is how I can remove the tab bar item.
UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
NSMutableArray *tabBarViewControllers = [NSMutableArray arrayWithArray:[tabVC viewControllers]];
[tabBarViewControllers removeObjectAtIndex:0];
[tabVC setViewControllers:tabBarViewControllers];
//or to just disable it
NSArray *tbItems = tabVC.tabBar.items;
UITabBarItem *item_0 = [tbItems objectAtIndex:0];
[item_0 setEnabled:NO];

There is an option of hiding the bottom TabBar in the attributes inspector
"Hide bottom bar on push"
You might find this link useful
How to hide/show tab bar of a view with a navigation bar in iOS?

Related

How to create a tab bar on iOS?

I need on my main view controller to have a tab bar with tabs to navigate to all my other controllers. I just need the tab bar on this controller and when i get to another controller i just need to have a back button to go to the main controller.
Now i have some questions. I created the tab bar in the main view controller and all the tabs with the text and images that i need. However i see that i can only create IBOutlet for the tab bar and not IBActions for every tab(as i thought). So i created an IBOutlet and connected it to my tab bar.
How can i refer to every tab?
If i can refer to every tab how is it possible to change the view controller when a tab is selected when i cant use an action about it?(I am not asking for the code to change controllers , i am asking for the place that i should put the code so that my application knows that this specific tab was pressed and has to change controller).
Thank you for reading my post :D
You can create a UITabBarController programmatically in applicationDidFinishLaunching and set it as the root view controller (or if you prefer, you can present it as a modal view). Here is the minimal code to do it:
UITabBarController *tabBarController = [[UITabBar alloc] init];
UIViewController *controller1 = [[YourViewController alloc] init];
UIViewController *controller2 = [[YourOtherViewController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:
controller1,
controller2,
nil];
// set as the root window
window.rootViewController = tabBarController;
If you want to customize the look of the tab bar items, do so by adding overloading (UITabBarItem *)tabBarItem in the child view controller(s):
- (UITabBarItem *)tabBarItem
{
return [[UITabBarItem alloc] initWithTitle:#"Amazing" image:[UIImage imageNamed:#"Blah.png"] tag:0];
}
How to make a tab bar controller
by me
Drag tab bar controller into storyboard (hopefully you have one)
Control-drag from tab bar controller to each view you want hooked up to it
Pop bottles
Just so you know, this gives you the default tab bar controller behavior (so it will always be present and you can click from any page to another). If that's not what you want, then don't use a tab bar controller. To do otherwise is an abomination.
Storyboards are definitely helpful, but if you don't want to use one that's fine. Doing the Control Drag from the Tab Bar Controller to your new View Controller does indeed work (Dustin's response).

iOS Left "More" UIBarButtonItem (in navigation controller) if UITabBar is hidden

In my ios5 application I have a TabBar, and for each tab I have a navigation Controller.
I have more than 5 tabs, but if I set HIDDEN to YES to my tab bar a UIBarButtonItem with text "More" will appear in my navigation bar (leftItemsSupplementBackButton is set to YES) to the left - only in rootViewControllers, and there's no way to get reference to this button.
Pressing this button it will go to moreNavigationController.
Any ideas how to hide this button?
The More Button is the back button standard will appear automatically if one of the > 4 tab bar item is selected, even if tab bar is hidden. The workaround to get hidden this button is to hide, only if view controller is rootviewcontroller, the back button:
self.navigationItem.hidesBackButton = YES;

how to use multiple tab bar controller in an application?

I have several buttons in root view and I want to switch different tab bar controller when click each button. How can I do this?
I did for just first 'Denetim Formu' button. When click it it show second picture. But How can I do like this for other 'Lokasyon, Taahhüdname, Form Sorgulama, ect' buttons. Each have to show different tab bar controller.
What you can do is on your particular button click:-
1.)Which tab bar button item and its view controller you want to change(identify it).
*YourViewController* *import=[[[*YourViewController* alloc]initWithNibName:#"*YourViewControllerNibName*" bundle:nil] autorelease];
UINavigationController *baseNav = [[[UINavigationController alloc] initWithRootViewController:import]autorelease];
NSArray *arr=[[appDelegate tabBarController]viewControllers];
NSMutableArray *array=[NSMutableArray arrayWithArray:arr];
[array replaceObjectAtIndex:1 withObject:baseNav];
[[appDelegate tabBarController]setViewControllers:array];
appDelegate.tabBarController.selectedIndex=1;
UITabBarItem *item= [[[[appDelegate tabBarController]tabBar]items ]objectAtIndex:1];
item.image=[UIImage imageNamed:#"hometab.png"];
item.title=#"Home";
By this way you can change your tab bar view controller tab with its item name and image.

Top Bar does not appear for presentModalViewController

I've created a UIViewController subclass called addItemToListViewController. I selected add an "xib" as well, and just created a simple page with a couple of labels and a textField. In the interface builder I selected "Top Bar - Navigation Bar" so that when it is put on the stack when the application runs it will have a top bar that will match the initial main window. In the Interface builder it shows the top border, but when I run the application in the simulator the top bar is not present once the view is displayed.
Here is the code I placed in the rootViewController to present the view controller
- (IBAction)addButtonPressed:(id)sender
{
AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: #"AddItemToListViewController" bundle:nil];
[self presentModalViewController: AddItemToListViewController animated: YES];
[AddItemToListViewController release];
}
I'm only able to have the top bar present if I manually add a Navigation bar to the xib. If I must add a Navigation bar to my xib, what is the purpose of the "Top Bar" attribute?
- (IBAction)addButtonPressed:(id)sender
{
AddItemToListViewController *addItemToListViewController = [[AddItemToListViewController alloc] initWithNibName: #"AddItemToListViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addItemToListViewController];
[self presentModalViewController: navController animated: YES];
[AddItemToListViewController release];
[navController release];
}
That "top bar - Navigation bar" in InterfaceBuilder is what's known as a "Simulated Metric". It's there to help you lay out your view with correct spacing when other visual elements - the status bar, navigation bar, or tab bar - might consume some of the device's screen real estate. It doesn't actually do anything other than shrink the vertical dimensions of the view defined by the NIB. The purpose is to help you layout your view, not to actually create a component that will appear in your app.
If you want a navigation bar, then you have two choices. The first choice is to use a navigation controller (of which your initial view will have to be the root) and call
[self.navigationController pushViewController:newVC animated:YES];
The process of setting up a navigation controller correctly, etc, is nontrivial, and you should do some searching to find the best way to do that for your app. For a simple app, especially if you're just learning iOS, you can use the "Navigation-based Application" template when you create a new project. With a navcon, you get all the fancy behavior normally associated with that top bar - an automatic back button, fancy left/right scrolling when you transition to a detail view, etc.
The second option is to put a "fake" navigation bar in the detail view, using the Navigation Bar object. You can find that object, plus some other related objects, in the bottom half of the "Utilities View" (the right-most pane) in XCode. Just drag the object into your XIB and blammo, you have a 44-pixel tall gray bar. This navigation bar is just like what you get when you use a Navigation Controller except you don't get the stack functionality; you can still add buttons to the left and right, change the title, tint it to a specific color, etc.
The xib does not know you will use the controller as a modal view as it could also be used for a normal view which could show a top bar. Only when you push the view it will use or ignore the showing of this top bar.
In short: its there in case you will use the xib for a normal view :)

Objective C: How to switch from one Tab bar to another via program

I have 5 different tabs in my tabbar controller.
My intention is to be able to switch from one tab bar via code. For example I am currently in the 5th tab of the app and when I click on the 'done' button, the app should switch my view to the rootview controller belonging to the 1st tab.
Any advise on how I can do this?
Set selectedViewController property of UITabBarController:
self.myTabBarController.selectedViewController = myViewController;
Use as below
self.myTabBarController.selectedViewController
= [self.myTabBarController.viewControllers objectAtIndex:0];
Heres a simpler answer (if you know the index of the Tab Bar item is not in the "more" view controllers):
just get a reference of the tabController and set the "selectedIndex" property
self.tabBarController.selectedIndex = 0;
Reference: https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html#jumpTo_6