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.
Related
I think my title is what i am looking for. So, i have a tab bar with 4 view controllers. I started to configure my app within storyboard and i configured my tab bar controller from there. I, also, have declared the names of the view controllers in my appdelegate file. My question is the following. Can i add a button in the middle of my tab bar controller? I searched a lot about that and i didn't find a complete solution. Can i use storyboard to connect my view controllers to the tab bar and also app delegate to declare that button to the center of tab bar? Please help. This is my code so far in app delegate file (anything else is in storyboard, juts the view controllers connected to tabbarcontroller) :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Assign tab bar item with titles
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = #"Tab1";
tabBarItem2.title = #"Tab2";
tabBarItem3.title = #"Tab3";
tabBarItem4.title = #"Tab4";
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"maps.png"]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"TAPBARBGR.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tabbar_selected.png"]];
// Change the title color of tab bar items
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, UITextAttributeTextColor,
nil] forState:UIControlStateHighlighted];
return YES;
}
Thank you.
If you connect 4 pages to the standard UITabbarController it will layout 4 buttons equally dividing the space between them. You don't have any control how they are laid down. You can rearrange them in the storyboard or in the code but with 4 buttons - no button will be in the middle.
You can add 5th button and rearrange it to the 3rd position, so this one will be in the middle of tabbar. Or you can look for 3rd party custom tabbar implementations - there are bunch of them on gitnub and other places. Some of them would have specific middle page that styled differently from others.
Can i add a button in the middle of my tab bar controller?
No.
Assuming that you mean adding a button different from the other tab bar items, then no, UITabBar doesn't provide for this. As explained in the UITabBar documentation, all items in a tab bar are expected to be tab bar items, but you could consider a UIToolbar instead:
Each button in a tab bar is called a tab bar item and is an instance
of the UITabBarItem class. If you instead want to give the user a bar
of buttons that each perform an action, use a UIToolbar object.
From a user experience point of view (IMO) it's probably not a good plan to try to mix tab bar and tool bar functionality.
I have this structure:
When I want to add a ´UINavigationItem´ from the storyboard the navigation bar is "disabled", so I tried to add the right button programatically:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(showPickerView:)];
self.navigationItem.rightBarButtonItem = button;
But nothing happens. I renamed the navigation bar title from storyboard, but when I run the app the title is not set. I really don't know what is the source of the problem. There is just the back button that is appearing.
Thank you for your help.
Each item in the tab bar controller should have a navigation controller as the root controller (well, you don't need all of them to have a nav controller if you don't need them). What you currently have is a tab bar controller in the navigation controller (unless it's modal) so the view controllers contained in the tab bar controller can't see out to the navigation controller.
As other answers have pointed out, the hierarchy of your UINavigationViewController is incorrect. The UITabBarViewController will always be the parent of your UINavigationViewControllers.
For each UIViewController that is a "Tab", you need your UINavigationViewController to be "in front" of each "tab" UIViewConrolller that will have segues.
So you do not need a UINavigationViewController for every "tab" UIViewController, as someone else pointed out on this thread, only for "tabs" that will segue to other Views that you want to track navigation with a UINavigationViewController
The hierarchy can be seen in #App Dev Guy's answer here
In Swift 2, Xcode 7 has a very handy feature for adding a UINavigationController:
Select the UIViewController that is being used as a "tab" for the UITabBarNavigationController
On the top Xcode menu, select "Editor" ->
"Embed In" ->
"Navigation Controller"
I tried to reproduce your problem.
I started a new 'Single View' project.
Selected the iPhone storyboard
Selected the view controller
Menu -> Editor -> Embed in -> Navigation Controller.
Open the object browser
type 'Bar Button Item' in the search bar
draw the bar button item over the navigation bar.
Done. See if you can do the same.
Try it like this. It's a little different than how you are doing it and it works for me:
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithTitle:#"title"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(showPickerView:)];
self.navigationItem.rightBarButtonItem = button;
If you want to do this in the Storyboard, you don't drag the "Navigation Item", but the "Bar Button Item" instead.
I have created application using storyboard and has TabBarController with 5 tabs.
Each tab has tabicon and tab title. When a tab is selected I want to change the tabbar icon.
How can I do using storyboard?
- (void)setFinishedSelectedImage:withFinishedUnselectedImage: is deprecated. If you're using storyboards, it's as simple as
UITabBarItem *tabBarItem0 = [self.tabBar.items objectAtIndex:0];
UIImage* selectedImage = [[UIImage imageNamed:#"settings-active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem0.selectedImage = selectedImage;
EDIT
In Swift:
var settingsItem = self.tabBar.items?[0] as UITabBarItem
settingsItem.selectedImage = UIImage(named: "home-selected")
Note that this code belongs in the viewDidLoad override of your UITabBarController subclass.
I have got it.
Subclass UITabBarController - MyTabBarController
Over write viewDid load :
write
UITabBarItem *tabBarItem0 = [self.tabBar.items objectAtIndex:0];
[tabBarItem0 setFinishedSelectedImage:[UIImage imageNamed:#"selectedimage.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"image.png"]];
like this set for all the tabbar items and in story board set the tabBar controller to MyTabBarController. It's working fine.
You can now do this easily in storyboard. On each tabviewcontroller that you have, it should contain a Tab Bar Item in the hierarchy (looks like a little blue star), Click on this and the settings on the right should look like the image below. The tab bar title & image can be changed here.
Below code will change tabbar image in selection:
UITabBarItem *tabBarItem = [[tabbar items] objectAtIndex:0];
[tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"img_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"img.png"]];
change identifier to custom and add image
I am looking at changing the graphics on a tabbar, such as
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
tabBarItem1.title = #"Home";
tabBarItem2.title = #"Maps";
tabBarItem3.title = #"My Plan";
tabBarItem4.title = #"Settings";
The problem I have here is that my tabbarcontroller is not my root view, so how can I reference the tabbarcontroller to change the tab images?
I am following a suggestion from this post (Can I have more than 1 UITabBarController?) that refers to having a tableview that links to one or more tabbarcontrollers.
So my root view is not a tab bar, but the tab bar view is loaded after coming from one previous screen.
I have this all working, with the initial screen and then the tab bar, and everything is working fine, I just need to change the graphics on the tab bar, and am not able to do this as all tutorials on changing tab bar graphics use the app delegate and refer to the tabbarcontroller as the root view.
Any help on this greatly appreciated!
Tab bar items belong to the individual content view controllers in each tab, so rather than trying to reference the tab bar controller, you should change the tab item properties in those controllers. If you want those changes to be visible as soon as the tab bar controller comes on screen, you need to put those customizations in their awakeFromNib or initWithCoder methods (for controllers set up in IB).
Richard,
Firstly, the post you reference pointed out that this is very bad visual design, but anyway, if you need to do it.
How you reference the tab bar controller depends on how you instantiate it. This stuff should be fairly obvious, so I'm guessing that you're working with xibs or storyboards.
Either way, when you load the second view that is going to have the tab bar on it, you'll be loading a view controller to control the view. Just bind the tab bar to a property of the view controller in IB, and you can do your set up in the view did load of the view controller.
Actually, if you're using IB ( XIB or storyboards), you can just set up your icons there.
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.