Safe way to remove tab bar from UITabBarController in ios - ios

I want to use UITabBarController but I do not need a tab bar because I'm going to switch tabs from the menu in another view controller. I want to remove it as it will be never used. I've created a subclass of UITabBarController and put this code:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tabBar removeFromSuperview];
}
Is that safe to remove tab bar that way?
Update: Why tab bar controller?
Apple suggests to use standard container view controllers whenever it possible. I'm trying to follow that. My screens workflow is the same as for tabs, except that they are switched by left side menu, not by the tab bar.
When I use a UIViewController and change it's child view controller I've got a lot of problems when autolayouts are not working properly.
The other reason is that I want to use story board, rather then create segues from the code so I can see my app workflow easily.

When you are saying, you don't want to use it, then why to remove it. You can go with a
tabbarcontroller with hidden tabbar. I am switching tabs from the bottom custom bar.
I am doing an app, which has a tababr controller with three items. Instead of using system tab, I am creating a custom view at the footer like tab & have actually set the hidden property of tabbarController's tabbar to YES.
myTabBarController.tabBar.hidden = YES;

Try this code
[app.navigationController.view removeFromSuperview];
[app.tabbarcontroller.view removeFromSuperview];
[app.window addSubview:app.navigationController.view];
[app.navigationController popToRootViewControllerAnimated:YES];

Related

Unable to configure UINavigationBar for view embedded in Tab Bar Controller

I a writing an app (iOS8) that ultimately needs to load a UITabBarController via a segue from a UITableView. For the most part this setup can be done via Storyboards and works as expected, however I would also like to add a UIButtonBarItem to the destination view which is where the problems start.
A setup that works (without a UITabBarController) can be configured as follows
The button uses a "Show" segue to display the final view controller
The second UIBarButtonItems are added by copying over the Navigation Item from the first view controller (How to add buttons to navigation controller visible after segueing?)
If I run this in the Simulator, everything works as expected and I see both the back button and the desired "Add" UIBarButtonItem:
If I then embed the final view controller in a UITabBarController, the UIBarButtonItem I added disappears and so far any changes I have made to the storyboard setup (adding a UINavigationController in between the UITabBarContoller and the last view for example) or attempts to add the UIBarButtonItem programatically don't make a difference:
Is there anyway to get the final setup working with both a UITabBarController and UIBarButtonItems?
I have the same setup in one of my apps and it works fine. Not sure why you are having issues, but I did add a few lines of code in my custom Tab Controller that may help you. I think the issue is that the nav bar from the original navigation controller is still being shown, so subclass UITabBarController and put these lines in viewWillAppear:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationItem setHidesBackButton:YES];
[self.navigationController setNavigationBarHidden:YES];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
In my app, the views before the tab bar controller were login/register views, so there was no reason to navigate back to them after entering the tab controller "stack," but I'm sure it won't be difficult to add a back button that accomplishes this. I believe you only need the [self.navigationController setNavigationBarHidden:YES]; line, which only hides the nav bar instead of hiding the back button or disallowing the pop gesture.
I know this is late but I just want to add swift 3 code.
The reason being that the NavigationBarA of the tabBarController is hiding your NavigationBarB that sits in between your tabBarController and the final ViewController. So you just have to set to hide the NavigationBarA
in viewWillAppear of your final ViewController you can add the following (without a need to subclass tabBarController)
self.tabBarController?.navigationController?.setNavigationBarHidden(true, animated: false)

Fixed top menu bar

I'd like to implement a top menu bar in an iOS app. I want it to be shown in every view controller. Whats the best approach besides using the bar from the navigation controller?
There are built in functionalities for iOS that allow you to have UI persist amongst many view controllers. Currently the UITabBarController and the UISplitViewController contain views at the bottom or left and persist among the other view controllers that are linked to be displayed. The UINavigationcontroller has a view at the top. If you wanted something else, then there is no standard iOS way of doing it, but here are 3 options.
Create a master view controller class that all class inherit from this one. Have what ui you would like to persist recreated via a singleton and added as a subview when you allocate the master class.
The top bar could be added as a subview to the UINavigationController. Subclass uinavigationcontroller and you can do [self.view addSubview:view].
Or add the ui to the window of the app. This can be done anywhere through out the app all you need to do is UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview:view];
I would suggest going with option 2, option 1 is too tedious for something so simple and option 3 would persist through out the entirety of the app, option 2 associates the view you add with the UINavigationController, therefore only the view controller in the navigation controller are affected, like wise if you remove the navigation controller the content goes with it.

Navigation title not showing on view with tab view controller, but "back" navigation works

I'm relatively new to iOS Objective-C development, and I've come across a problem that I can't find a solution to.
I have a Table View Controller, with two prototype cells on it, which populate fine. This Table View Controller is one of three Tab Views, and the View that sends to the Tab Views has a Navigation Controller. This means that the views within the Tab Views also have a Navigation bar. The bar works fine, in terms of the "back" button working as expected, and the bar being in position. However, (at least on the List View) the Navigation Bar isn't fully recognised - it's title doesn't appear, and the table cells start directly below the status bar, rather than below the navigation bar.
Here's a couple of screenshots showing the problem:
what appears in Xcode (what I expect to happen)
And then on the device, this is what actually appears - the Back button in place and working fine, but no title field, and the table cells start too high.
I've tried adding Navigation Bar's and Navigation Items, and while adding a Navigation Item allows me to put a title on in Xcode, it still doesn't appear on the device in testing. I also tried to add another Navigation Controller just before this view, but that didn't resolve the issue either, and it caused navigation problems further down in the heirachy.
Hope I've been clear enough, please say if I need to post more information - I'm relatively new to Xcode and so not sure what exactly is applicable and what isn't. Thanks!
please try this code, it might fix your table position
// Since in iOS7 the nav bar is translucent by default, so the table view starts at (0,0)
// you can either disable the translucent, which i don't recommend unless you really want to
// or just add 64 pixel on the top of your table view
[self.YOURTABLEVIEW setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];
and for the title, please try this
self.tabBarController.navigationItem.title =#"YOUT TITLE NAME";
Hope that helps..
Assuming your hierarchy as
NavigationController -> ViewController -> TabBarController -> ViewController1
-> ViewController2
-> ViewController3
If you want to hide navigation item in viewcontroller1, Add the following line
self.navigationController.navigationBarHidden = YES;
If you want to show title in viewcontroller2, Add the following line in
self.navigationController.navigationBarHidden = NO; //add this if you hide navItem viewcontroller1
[self.parentViewController.navigationItem setTitle:#"Title"];
If you want to hide backbutton and show title in viewcontroller3, Add the following line
self.navigationController.navigationBarHidden = NO;
[self.parentViewController.navigationItem setTitle:#"Contacts"];
self.parentViewController.navigationItem.hidesBackButton=YES;
Add this lines to viewdidAppear method instead of ViewdidLoad ,if you have problems inshowing when switching tabs.
I had the same problem, but what I did to create this problem was that my buttons action was connecting to the actual table itself and not the table Controller. I removed the modal action and created a new action to the table controller and it fixed the problem.
Try to click the Navigation Bar from your storyboard or nib.
Then add your title to the property.

TabBarController - switch tab keeping banner view;

I have a TabBarController with four view controllers . In my first view controller i have the following two methods:
-(void)loadclistview
{
[self.tabBarController setSelectedIndex:2];
}
-(void)loadglistview
{
[self.tabBarController setSelectedIndex:1];
}
I use them to change the view form one tab to the other. But changing tabs this way doesn't preserve my banner view, it does not load the banner into the new view controllers (tabs). If i change tabs using the tab bar , all the views load banners correctly.
Pls help me here - i don't know why this is happening , and what should i do to switch tabs through selectors so that the banner will load in the new views.
EDIT:
I solved it by adding the current bannerview as a subview to the selected viewcontroller from the tab bar ; like this:
[self.tabBarController.selectedViewController.view addSubview:_bannerView];
I saw this also works to adding the bannerview to subsequent view to a navigationcontroller - like showing the banner in child views of the main view which constitues a tab;
[self.navigationController.view addSubview:_bannerView];
Adding the subview this way is fine, but you also have to handle the show/hide of it when there is or isn't an iAd, and the resize of the content view to make room for displaying the subview. The TabbedBanner project in the Apple iAdSuite sample has a nice encapsulation of this functionality.

iOS - Back button on selecting a tab

I have a UITabBarController. When I select a tab other than the first, I want the tab bar hidden (which I achieved by hidesBottomBarWhenPushed) and the navigation bar to have a back button (not a plain, but default arrowed) (to get to the first tab). How do I achieve this?
I'll answer your question below, but I have to say that this is a very non-standard user interface which is mixing metaphors. If you really want to push to different view controllers, you really should not be using a tab bar. You probably should just have simple buttons to go to your secondary view controllers. I wouldn't be surprised if Apple would reject an app that used a tab bar like the way you're conceiving of it.
Anyway, to answer your question, you don't want to try to add a "back" button to the secondary views, but rather you just want to properly implement a navigation controller. Then, when you push to the secondary views, you get the back button automatically. And you also don't have to worry about silliness associated with hiding the tab bar, because when you push to another view, it will be pushed off, too.
So, consider this example, where you have a tab bar with buttons "One", "Two" and "Three". But, rather than being a proper tab bar controller configuration, you'd have something like this, where "One" is on navigation controller and has a simple tab bar user interface control on it, and if you happen to tap on the second or third tab, it will just push to the respective view. Thus, logically, it might look something like this (even if you're not using storyboards, this might help visualize the potential configuration):
So, on the main view controller ("One"), you'd add a tab bar with an IBOutlet:
#property (weak, nonatomic) IBOutlet UITabBar *tabBar;
I'd then configure that tab bar in viewDidLoad, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBar.delegate = self;
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}
Clearly, the view controller itself would need to be a UITabBarDelegate, and you'd need a didSelectItem that would perform either a push segue or use your navigationController to pushViewController. And, clearly, you will want to change the references to #"Two" and #"Three" with the actual labels you put on your tab bar buttons, but hopefully this gives you the idea:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if ([item.title isEqualToString:#"Two"])
{
// put code for pushing to second view controller here
}
if ([item.title isEqualToString:#"Three"])
{
// put code for pushing to third view controller here
}
}
I'd also want the main view controller to make sure to set the selected item back to the first item when it reappears after you pop back:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}
Anyway, if you do it this way, there's no hiding of the tab bar (because it's no longer a controller, but rather just a user interface element on the first tab). This is probably the easiest way to implement your desired user interface.

Resources