Change Image of UItabbar Item, Using storyboards - ios

I have the following story board:
As you can see there is a tab bar application with 5 tabs, on the storyboard I've assign the logo for each tab. Now when the user clicks a cell in a particular view I want to change the image of one of the tabs. How can I do this? I don't have an instance of the tab bar view controller or items since storyboards pretty much does all this for me. So my question is what methods do I have to implement to change the image? If I need the tab bar controller how can I get its instance and in which class should I point it to?
Thank you very much,

In any UIViewController class that is part of the Tab Bar hierarchy, all you have to do to get in instance of the tab bar controller is:
//In UIViewController
UITabBarController *tabBarController = self.tabBarController;
You can then change the image as so
//Suppose you want to change the 1st (0th) tab bar image
UITabBarItem * tabItem = [tabBarController.tabBar.items objectAtIndex: 0];
tabItem.image = //whatever image you want to change to

Each UIViewController has a property called tabBarItem which is a UITabBarItem that the tab bar controller uses to set the image representing that controller. You can manipulate that to change the image of the controller in question.

I found that -- at least in Xcode 6.1.1 using Swift -- the direct manipulation of the tabBarItem did not work for me.
However, #borrrden's answer put me on the right track. Apple's documentation for UITabBarController states pretty clearly:
You should never access the tab bar view of a tab bar controller
directly. To configure the tabs of a tab bar controller, you assign
the view controllers that provide the root view for each tab to the
viewControllers property.
...
Tab bar items are configured through their corresponding view
controller. To associate a tab bar item with a view controller, create
a new instance of the UITabBarItem class, configure it appropriately
for the view controller, and assign it to the view controller’s
tabBarItem property.
Therefore, in accordance with that, below is what I came up with that did work for me.
It's written in Swift, and I hope that future readers can translate it accordingly if they need to (I also changed the image names to be super-generic).
I also used UIImage's imageWithRenderingMode method so I could use custom images instead of the shadowy silhouette default images that iOS creates (I would like to credit #NSHeffalump's answer here for that...).
if let viewControllers = tabBarController.viewControllers as? Array<UIViewController> {
var tabBarItemImageNames = ["TabBarItemImage0","TabBarItemImage1","TabBarItemImage2","TabBarItemImage3","TabBarItemImage4"]
var vcIndex = 0
for vc:UIViewController in viewControllers {
let selectedImage = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let image = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var tabBarItem = UITabBarItem(title: "", image: image, selectedImage: selectedImage)
vc.tabBarItem = tabBarItem
vcIndex++
}
}

Related

UITabbar View overlap title on image after present the another UIViewController

I am using the UITabbarview with for item. For every tabbar item I am using the image and title.
Its perfect when I come that TabbarViewcontroller screen and navigate to another to anther UIViewcontroller by push.
But if i navigation to another UIViewController by present then come back to same TabbarviewController screen then title overlap on image of bar item.
like the images
Now I am not able to understand, why this happening and how to resolve this issue.
use this code in your "viewWillAppear" method.
-(void)viewWillAppear:(BOOL)animated
{
[UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(0, -3);
}
Select tab bar icon and choose image inset all = 0

How to embed in Navigation Controller to a tabbar controller

My initial view controller is a tab bar controller.I want to make the tab bar at top insted of bottom(which I have done using self.tabBarController?.tabBar.frame) . I want to make a navigation bar appear above the tab bar . Can anyone please hep me to do this
A UITabBar should always be at the bottom of the screen.
There is third party implementations of something that is similar to android tabs and might be what you are looking for.
Take a look at https://github.com/HighBay/PageMenu for example.
You can instantiate your view controller with the one of these codes
let VC1 = self.storyboard?.instantiateViewControllerWithIdentifier("storyboardID") as! DemoViewController
OR
let VC2 = YourViewController()
If your view controller is defined at the interface bulider , then go with the first else , go with the second.
Now to construct view array for tabBarController I put NavigationController as the element with rootViewController being the VCs I instantiated .
let tab1 = UINavigationController(rootViewController: VC1)
let tab2 = UINavigationController(rootViewController: VC2)
Describe the tabBarItem images like this
tab1.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "unselectedImage")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "selectedImage")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal))
Finally describe the tabBar array
self.viewControllers = [tab1,tab2] //This will create tabBar with 2 tabs
Your navigation controller will appear at the top with this. You can customize your Navigation Bar. For that see this.
Also don't forget to make your tabBarController the rootViewController in the AppDelegate. Otherwise you will go against Apple guidelines.
Hope this helps. :)
My suggestion is that make viewcontroller and use the UISegmentController to perform the Tab Bar Actions below the Navigation Bar..
thank you

Replace one ViewController in TabbarController with NavigationBar

I noticed in Whatsapp there are four tabs. If you disable the access to Contacts, the view in first tab, Favorites, will be replaced with the information view which will guide user to open the Settings.
I was trying to copy this function with following codes (in AppDelegate):
let gotoSettingsVC = UIApplication.sharedApplication().keyWindow?.rootViewController!.storyboard?.instantiateViewControllerWithIdentifier("gotoSettingsViewController") as! GotoSettingsViewController
let tabbarController = UIApplication.sharedApplication().keyWindow?.rootViewController! as! UITabBarController
gotoSettingsVC.tabBarItem = tabbarController.tabBar.selectedItem
tabbarController.viewControllers![tabbarController.selectedIndex] = gotoSettingsVC
The gotoSettingsVC can be shown correct but without Navigation Bar. How to show this View with a Navigation Bar like the normal View in tabbarController?
thank you for any help.
Embed the GotoSettingsViewController in a navigation controller, and set that as one of the tabBar controllers view controller.

Change Split View Controller Navigation Bar Colour

I have a split view controller and I want to change the navigation bar colour.
Using the below in app delegate I have changed the colour of the detail screen (right).
navigationController.navigationBar.barTintColor = UIColor.orangeColor()
But I can't seem to figure out how to change the bar of the navigation controller (left).
Can anyone help?
Thanks
The template provides you a MasterViewController (the left) and a DetailViewController (the right). It is the same code in both the MasterViewController and the DetailViewController, likely in viewDidLoad. You have it right, although there is an optional you are missing:
if let navContr = self.navigationController {
navContr.navigationBar.barTintColor = UIColor.orangeColor()
}

How to set first TabBar selected programmatically on iPhone

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

Resources