I'm developing an app that has iOS 7.1 as its Deployment Target. So far everything works just fine, however there is one huge issue, the tab bar images are not showing up. This is confusing, because they show up when running on the iOS 8 Simulator, I'm using the Xcode 6 beta 1.
I've set up two images. The dimensions are 60x60 and 70x60. So size does not seem to be an issue. I've set the images up in Interface Builder. When logging the tab bar image to the console however, it returns nil. I've then tried setting the images up in code, and using the same method to log it to the console, I now have a memory address for the image, but it still does not show up.
When searching for an answer on Google and stackoverflow, I've found this method:
self.tabBarItem.image = [[UIImage imageNamed:#"IMAGE"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.selectedImage = [[UIImage imageNamed:#"IMAGE"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSLog(#"%#", self.tabBarItem.image);
This worked for some users apparently, but not for me. I can not figure this out, I'd be glad if someone could help me.
I have a feeling about what you're doing, but it's still not clear.
You have a tab bar, but you have an outlet to the tab bar item itself.
That's not the way you would do it. You create an outlet to the tab bar, then add custom items with:
UIImage *defaultImage = [UIImage imageNamed:#"ImageName"];
UIImage *selectedImage = [UIImage imageNamed:#"ImageName"];
UITabBarItem *itemZero = [[UITabBarItem alloc] initWithTitle:#"Item One" image:defaultImage selectedImage:selectedImage];
UITabBarItem *itemOne = [[UITabBarItem alloc] initWithTitle:#"Item Two" image:defaultImage selectedImage:selectedImage];
NSArray *items = #[itemZero,itemOne];
[self.tabBar setItems:items animated:animated];
Related
I have a previously existing app (pre ios8) that uses UITabbar. The tabbar is visible in ios7 simulator and device, but it is invisible in ios8. What is causing this issue? the space for the tab bar is there, but its background and text/images are not there. i've attached a pic of it.
iOS 7:
iOS 8:
Even if setFinishedSelectedImage:withFinishedUnselectedImage: is deprecated in iOS7, it is working fine in iOS7 but not in 8.
Use image and selectedImage property of UITabBarItem instead.
I also had the same issue but my problem was different.
Reference code:
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
if ([self iOS7OrAbove])
{
//use UIImageRenderingModeAlwaysOriginal to set the custom image for ios 7 and above.
tabBarItem1.selectedImage = [[UIImage imageNamed:#"SelectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.image = [[UIImage imageNamed:#"UnselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
else
{
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"SelectedImage"] withFinishedUnselectedImage:[UIImage imageNamed:#"UnselectedImage"]];
}
At some point during runtime, I want to change the image for one of the tabs in my UITabBar. Here is what I have tried so far:
[[self.tabBarController.tabBar.items objectAtIndex:1]
setImage:[UIImage imageNamed:#"image-name"]
forState:UIControlStateNormal];
The above gives me a -[UITabBarItem setImage:forState:]: unrecognized selector sent to instance
If I use the setImage method without forState it works, but this method was deprecated in iOS 3.
I tried your answers, but now there's this weird blue line above the UITabBar's UIIMage I changed. Any idea why?
Use image and selectedImage properties:
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
item.image = [UIImage imageNamed:#"image"];
item.selectedImage = [UIImage imageNamed:#"selected_image"];
Also pay attention on this:
By default, the actual selected image is automatically created from
the alpha values in the source image. To prevent system coloring,
provide images with UIImageRenderingModeAlwaysOriginal.
UITabBarItem extends UIBarItem which has an image property.
Do:
[[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:#"image-name"]];
Though it would be easier to read and debug as follows:
UITabBarItem *item = self.tabBarController.tabBar.items[1];
item.image = [UIImage imageNamed:#"image-name"];
though the question is very old and the answer by #visput absolutely working but for some one complete newbie like me
In your UITabBarController implementation
if while loading of the UITabBarController you want, put it in viewDidLoad method
UITabBarItem *item = [self.tabBar.items objectAtIndex:ITEM_INDEX];// item index is the tab index which starts from 0
item.image = [UIImage imageNamed:#"image"];
item.selectedImage = [UIImage imageNamed:#"image"];
If you want to change it runtime like on selection of something, put this code in viewDidLayoutSubviews method
I have created a UITabBarcontroller programatically as follows:
UITabBarController *tbc = [[UITabBarController alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5,nil]];
which works ok. For each of the UIViewControllers, i want to set their tabbaritem.image to a specified image so i do this:
UITabBarItem *tbi1 = [[UITabBarItem alloc] initWithTitle:#"Search" image:
[UIImage imageNamed:#"tab_bar_search_50_50.png"] tag:0];
vc1.tabBarItem = tbi1;
and i have also tried
vc1.tabBarItem.image = [UIImage imageNamed:#"tab_bar_search_50_50.png"];
and
UITabBarItem *tbi1 = [[UITabBarItem alloc] initWithTitle:#"Search" image:
[UIImage imageNamed:#"tab_bar_search_50_50.png"] selectedImage:
[UIImage imageNamed:#"tab_bar_search_50_50.png"]];
The image size is 50x50 because according to apple-> https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html#//apple_ref/doc/uid/TP40006556-CH27-SW1
a tabbar icon of size "About 50 x 50 (maximum: 96 x 64) is ok". Yet when i run this in both the simulator and on a device (both retina screens) the image is way too large and hangs over the top of the the tab bar.
Ive even tried adding #2x as a suffix to the file name which doesnt work, but i thought since the app is building only for ios7 that these were no longer needed?
Im guessing the problem is either a bug in iOS7 or the issue lies with the image size, but i read in the documentation that if the image is too big it will be clipped to a certain bound?
I believe i have solved my problem. The problem was that when i added the #2x suffix, i also included this in the naming of the UIImage:
[UIImage imageNamed #"image#2x.png"];
But this needs to be:
[UIImage imageNamed #"image.png"];
I am new to ios development.
I created UITabbar programatically and set its delegate to self. All functions well. But my tabbar consists three tab bar items. I have given different images to the different tab bar items. But they all shows another image.
This is my code:
UITabbarItem item1 = [[UITabBarItem alloc] initWithTitle:#"item1" image:[UIImage imageNamed:#"imagename"] tag:1];
Try this :
UIImage *selectedImage0 = [UIImage imageNamed:#"tab-selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"tab-unselected.png"];
[item1 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
I'm developing a shopping cart tab. Originally I just use the default badge value to show how many items in the cart on the bottom tabbar. Now the designer wants to be fancy, he wants to show different image based on how many items in the cart. For example, if there's one, show cartTab-1.png, if 2, show cartTab-2.png...
I tried to change the tabaritem (UITabBarItem)'s image but it didn't work for me. Is it feasible? I discussed with my colleague, he said I may have to draw the image on top of the tabbarItem by myself. Do you have any suggestion? Thanks
more details:
I created the tabItem using InterfaceBuilder, and set the image and title over there
I need to support ios4. So I can't use the setSelectedImage...
In my case it's a KVO, if the cart count changes, it notifies the method to update the image. not in the initialize step.
does anyone know why [self.tabBarItem setImage:[UIImage imageNamed:#"cartxxx.png"]] doesn't work? When I debug, the property do changed, but the UI remains same
Update
the below code works. Thanks everyone!
UIImage* cartTabImage = [UIImage imageNamed:cartTabImageName];
[[self.tabBarController.tabBar.items objectAtIndex:3] setImage:cartTabImage];
Swift 3.0 version for 2 tabs,
self.tabBar.items?[0].image = UIImage(named: "inactive_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[0].selectedImage = UIImage(named: "active_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].image = UIImage(named: "inactive_image_1")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].selectedImage = UIImage(named: "active_image_1")?.withRenderingMode(.alwaysOriginal)
UITabBarItem *tabBarItem0 = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem0 setImage:[[UIImage imageNamed:#"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem0 setSelectedImage:[[UIImage imageNamed:#"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
The simplest way I found was
self.tabBarItem.image=[UIImage imageNamed:#"myImage.png"];
Try this:
int numItems = 0; // count of items in your shopping cart
NSString *imageName = [NSString stringWithFormat:#"cartTab-%d",numItems];
// change your image
[[self.tabBar.items objectAtIndex:myIndex] setImage:[UIImage imageNamed:imageName]];
// or, if you want to set it when initializing the tabBar
UITabBarItem *item = [[[UITabBarItem alloc] initWithTitle:myTitle image:[UIImage imageNamed:imageName] tag:someTag];
This Answer May be Help You
UITabBarItem *i5=[[UITabBarItem alloc]initWithTitle:#"Profile" image:[UIImage imageNamed:#"profile.png"] tag:5];
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage
selectedImage is displayed when the user has selected the tab. unselectedImage is displayed when the user has selected a different tab.
in your viewDidLoad: do
UIImage *c1 = [UIImage imageNamed:#"cart1.png"];
UIImage *c2 = [UIImage imageNamed:#"cart1unselected.png"];
[[self tabBarItem] setFinishedSelectedImage:c1 withFinishedUnselectedImage:c2];
As mentioned on the updated question and other answers, in most cases the UITabBarItem needs to be accessed via the UITabBarController directly.
It seems that iOS creates a copy of the UITabBarItem instance, which explains why updating the self.tabBarItem property does not reflect the changes in the user interface:
My guess is that this happens when the Tab Bar items are created programmatically, instead of by the storyboard, but this is just a guess.
The solution is then, as pointed out, accessing the array of Tab Bar items via the Tab Bar controller. This solution is bad in that it depends on the knowledge of the tab bar item index:
UITabBarItem *tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem setImage:image];
[tabBarItem setSelectedImage:image];
Don't forget to update both images for default and selected states.
let favorites = UITabBarItem(title: nil, image:UIImage(named: "Screen Shot 2018-12-13 at 11.00.42 AM") , tag: 0)