It's possible select the UITabBarItem desired in a TabBarController with 4 UITabBarItem with a click on a button?
Sure. Just set the tab bar controller's selectedViewController or selectedIndex property to the desired view controller or tab index.
Related
I have a tabbar controller with 3 tabs, each tab has navigation controller, on root view controller of each navigation controller i want tabbar and on other view controller in same navigation controller i dont want tabbar.
Any solution?
set self.tabBarController?.tabBar.isHidden = true in viewWillAppear method of your controller when you don't want tabbar
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.tabBar.isHidden = true
}
Setting alpha value 0 to tab Bar hides all items and including tab bar
You should set the tabbar alpha to 0. This will hide the UITabBar.
However you need to set isUserInteractionEnabled to false since even when its hided, the buttons are there & clickable still!
Hope this helps!
Set Transculent property of Tab bar to true or check the same from storyboard
Set hidesTabbarWhen Pushed property of Tab bar to true or check the same from storyboard
Set Tab bar hidden true on view controller where you want Tab bar and set Tab bar hidden to false on view controller where you don't want Tab bar
I have a navigation controller, and its root view controller is a tab bar controller. In this tab bar controller I have two view controllers like this:
In the tab bar controller I have a custom navigation bar.
When I click the first item in tab bar, the navigation bar looks good
but when I click the second, I have a problem: below the navigation bar there is another navigation bar with red color.
Can some explain this for me?
If you don't want navigation bar of first navigation controller then from the interface builder (storyboard) select your root navigation controller (i.e. navigationcontroller that's embed with tabbarcontroller) and from attribute inspector uncheck shows navigation bar under Navigation controller! This will hide navigation bar for root navigation view controller!!
In your case you should hide and show navigation bar in viewWillDisAppear and viewWillAppear something like,
In viewWillAppear
self.navigationController.navigationBar.hidden = NO;
In viewWillDisAppear
self.navigationController.navigationBar.hidden = YES;
Do above things for your both viewcontroller of your tabbarcontroller!!
You can do navigationController.navigationBarHidden = true on the root navigation controller, or the child whatever suits you.
But the better will be if you use only one UINavigationController, and IMO navigationController of UITabBarController only.
I have a tab bar controller embedded in a navigation controller.
I want to set the title in the navigation from the view controllers inside the tabs.
If I use:
self.title = "ABC";
or
self.tabBarItem.title = "ABC";
the title of the tab at the bottom is set to "ABC", but not the title in the navigation bar at the top.
However, I only want to set the title in the navigation bar, not the tabs at the bottom.
How can I do that?
You can set it using self.navigationItem.title.
self.navigationItem.title=#"ABC";
You have to pass the navigationItem up the chain.
The UINavigationController shows the navigationItem belonging to its topViewController which is UITabBarController.
The UITabBarController shows the navigationItem titles in its tabs. So what you need to do is make sure that the tabBarController's navigationItem is it's selectedViewController's navigationItem
So to recap:
UINavigationController title = topViewController.navigationItem.title
UITabBarController tabTitle = selectedViewController.navigationItem.title
UIViewController title = navigationItem.title
You are probably using the wrong element, try with the following example :
self.tabBarController.title = #"YOUR_TITLE";
I have an application with a login system. I want the tabBarController to change dynamically at runtime if the user login himself successfully! I have 5 tabs (Accueil, Tous les Voyants, Inscription, Connexion, Aide).
When the user hit the login button, I want to change Inscription to Achat Jetons and Connexion to Profile and link another ViewController to both of theses tabBarItems!
Right now, I've successfully managed to replace the title and image logo of my tab bar. But I don't know how to link the viewControllers to them! Here's what I got right now:
- (IBAction)BTN_ConnexionClick:(id)sender {
UITabBarController *tabBarController = (UITabBarController *)self.tabBarController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:3];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"menu_iOS_achat.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"menu_iOS_achat.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"menu_iOS_profile.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"menu_iOS_profile.png"]];
tabBarItem1.title = #"Achat Jetons";
tabBarItem2.title = #"Profile";
}
I have created 2 new viewControllers via StoryBoard IB, I just don't know how to replace old linked viewController with new ones! Thanks for your help! :)
The mistake you're making is that you're changing the tab bar controller's tab bar's tab bar items directly. Don't! Change the tab bar controller's view controllers. The tab bar controller gets its tab bar items from them.
You might want to read my book on this topic:
http://www.apeth.com/iOSBook/ch19.html#_configuring_a_tab_bar_controller
Notice especially:
The tab bar controller’s tab bar will automatically display the tabBarItem of each child view controller
Do not do anything to mess that up! (You are messing it up.) Manipulate a view controller's tabBarItem. Manipulate a tab bar controller's viewControllers. Do not touch a tab bar controller's tab bar yourself.
I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view i.e. I am NOT using TabBarController.
My problem is on view load I need first tab get selected by default. Is there any property in TabBar which we can set to make it selected?
Thanks.
This code will work [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
In swift if tabbar is used not tabbarcontroller set default select
var tabbar:CustomTabBar?//if declare like this
tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem
or
let tabbar = UITabBar()//if declare and initilize like this
tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem
set the
tabbar.selectedItem=0; in the viewWillAppear so when ever the view appears it will select the first tab by default.
[self.tabBar setSelectedItem:self.tabBar.items[0]];
or
self.tabBar.selectedItem = self.tabBar.items[0];
The selectedItem property requires a TabBarItem and not an index. So provide the tabbaritem in index 0 for the first tab.
This is wrong then: tabbar.selectedItem=0;
You may select other tabs as well. Happy coding