Could not set tab bar image in Xcode 4.2 Programmatically

丶灬走出姿态 提交于 2019-12-12 05:31:56

问题


I created new tab bar project in xcode 4.2 and there is no app delegate xib file. Project has two navigation controller. I can set name of tab bar but couldn't set image. I am completely fed up with many tries. FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil]

firstView.title=@"Birthdays";
firstView.tabBarItem.image=[UIImage imageNamed:@"bottomleft.png"]; 
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil]; 
secondView.title=@"Settings"; 
secondView.tabBarItem.image=[UIImage imageNamed:@"bottomright.png"]; 
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondView];
    navController2.navigationBarHidden=YES;
    [viewControllers addObject:navController2];   

Here you can see what sort of output I am getting.


回答1:


Hard to tell from the code you posted, but you probably want to set the image on the tabBarItem of the UINavigationController (navController2), since it looks like that is what you directly add to you UITabBarController:

navController2.tabBarItem.image = [UIImage imageNamed:@"bottomright.png"];

The reason you can set the title is that navigation controllers by default automatically have the title of their current child view controller.




回答2:


It looks like you successfully replaced the left image, however it has no transparent areas. Did you follow the guidelines for designing the tab icon?

http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html

You need to use a white image on transparent background. If your background is not transparent, the whole image will just be a blue box, like the one on your left tab.

Example: To make a tab with a star on it, draw a completely white star on a transparent background. Do not use any other colors than completely white and transparent black.




回答3:


You never need to do this:

[UIImage imageNamed:@"bottomleft2x.png"]; 

UIImage will automatically determine whether the @2x is needed or not based on the screen scale, so make it simply:

[UIImage imageNamed:@"bottomleft.png"];

Unless, of course, your resource name is actually bottomleft2x@2x.png




回答4:


dude tab bar only allows white color and transparent background so if u want the picture to be like a birthday cake then draw a white birthday cake on a transparent background then add some transparent pixel in side the cake to create the looks of a cake instead of just the outline of the cake then u can set yr image to tab bar



来源:https://stackoverflow.com/questions/11385234/could-not-set-tab-bar-image-in-xcode-4-2-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!