How to set image in uitabbaritem for default - ios

I am working in UITabBarController, when I am going to set image for tabs, the image does not appear for first time when view is open. I don't know how to resolve this problem.
This is the code I am using:
UIImage *prfimage = [UIImage imageNamed:#"profile_o.png"];
UIImage *selimage = [UIImage imageNamed:#"profile_o.png"];
UITabBarItem *tbP = [[UITabBarItem alloc] initWithTitle:#"" image:prfimage tag:YES];
[self setTabBarItem:tbP];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:247/255.0f green:148/255.0f blue:29/255.0f alpha:2.0f]];
self.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.title = nil;

by following way you can set.
UITabBarItem *tabItem = controller.tabBarItem;
tabItem.image = [[UIImage imageNamed:#"history"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Related

How to set image on UITabbaritem?

I am working on UITabbarview controller, I set an image with the following code, but it is not visible the first time I open a view.
UIImage *prfimage = [UIImage imageNamed:#"profile_o.png"]; UITabBarItem *tbP = [[UITabBarItem alloc] initWithTitle:#"" image:prfimage tag:YES];
[self setTabBarItem:tbP];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:247/255.0f green:148/255.0f blue:29/255.0f alpha:2.0f]];
self.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.title = nil;
You should use this peace of code to set UIImage in your ViewController
[[[[self.tabBarController tabBar] items] objectAtIndex:0] setImage:[[UIImage imageNamed:#"yourImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]] ;//objectAtIndex:0 mean your first TabBar item You can do it for multiple.
It will help.Thanks

UITabBar Wierd Height Difference

I want the default height as seen in the Storyboard image when adding the bar programmatically. Any idea how to get it? Here is the code I use to add a UITabBarController to my app.
- (void)setCustomTabBar
{
//TODO: Fix TabBar height
UITabBarController *tabBarController = [[UITabBarController alloc] init];
TimelineVC *tvc = [[TimelineVC alloc] initWithNibName:#"TimelineVC" bundle:nil];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tvc];
TimelineVC *tvc2 = [[TimelineVC alloc] initWithNibName:#"TimelineVC" bundle:nil];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:tvc2];
tvc2.showFriends = true;
NotificationVC *nvc = [[NotificationVC alloc] initWithNibName:#"NotificationVC" bundle:nil];
nav3 = [[UINavigationController alloc] initWithRootViewController:nvc];
ProfileVC *pvc = [[ProfileVC alloc] initWithNibName:#"ProfileVC" bundle:nil];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:pvc];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setShadowImage:nil];
[tabBarController setViewControllers:#[nav1, nav2, nav3, nav4]];
tabBarController.selectedIndex = 0;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat tabBarWidth = screenRect.size.width/4;
[[UITabBar appearance] setBackgroundImage:[AppDelegate imageFromColor:[UIColor darkGrayColor] forSize:CGSizeMake(screenRect.size.width, 49) withCornerRadius:0]];
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:26/255.0 green:163/255.0 blue:133/255.0 alpha:1] forSize:CGSizeMake(tabBarWidth, 49) withCornerRadius:0]];
tabBarController.tabBar.translucent = NO;
UIImage *normalImage, *selectedImages;
normalImage =[UIImage imageNamed:#"TimeLineIcon"];
selectedImages = [UIImage imageNamed:#"TimeLineIcon"];
UIImage *normalImage2, *selectedImages2;
normalImage2 =[UIImage imageNamed:#"FriendsIcon"];
selectedImages2 = [UIImage imageNamed:#"FriendsIcon"];
UIImage *normalImage3, *selectedImages3;
normalImage3 =[UIImage imageNamed:#"NotificationIcon"];
selectedImages3 = [UIImage imageNamed:#"NotificationIcon"];
UIImage *normalImage4, *selectedImages4;
normalImage4 =[UIImage imageNamed:#"ProfileIcon"];
selectedImages4 = [UIImage imageNamed:#"ProfileIcon"];
//TODO: FOR LOOP
nav1.tabBarItem.selectedImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.image = [selectedImages imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav2.tabBarItem.selectedImage = [normalImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav2.tabBarItem.image = [selectedImages2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav2.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav3.tabBarItem.selectedImage = [normalImage3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav3.tabBarItem.image = [selectedImages3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav3.tabBarItem.badgeValue = #"1";
nav3.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav4.tabBarItem.selectedImage = [normalImage4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav4.tabBarItem.image = [selectedImages4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav4.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.window.rootViewController = tabBarController;
[self.window addSubview:tabBarController.view];
[self enableSupportKit];
}
[self.tabBar setFrame:CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height)];
This helped me.
Hope it helps.
checkout your launchImage.for example,it's correct in your simulator,but if you use your iphone(i guess is iphone6). remove iphone6 launchimage,then you will find the question.

iOS 7.1 and Xcode 5.1 issues with UITabBar

I have recently updated Xcode to 5.1 and now have issues with the tab bar. In iOS 7 and Xcode 5.0 i used this code:
// Graph tab icon
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *graphTabIcon = [tabBar.items objectAtIndex:0];
UIImage *noRenderGraph = [[UIImage imageNamed:#"graph"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[graphTabIcon setImage:noRenderGraph];
[graphTabIcon setTitle:#"Graph"];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
// Friends tab icon
UITabBarItem *friendsTabIcon = [tabBar.items objectAtIndex:1];
UIImage *noRenderFriends = [[UIImage imageNamed:#"group"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[friendsTabIcon setImage:noRenderFriends];
[friendsTabIcon setTitle:#"Friends"];
// Settings tab icon
UITabBarItem *settingsTabIcon = [tabBar.items objectAtIndex:2];
UIImage *noRenderSettings = [[UIImage imageNamed:#"settings"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[settingsTabIcon setImage:noRenderSettings];
[settingsTabIcon setTitle:#"Settings"];
// Info tab icon
UITabBarItem *infoTabIcon = [tabBar.items objectAtIndex:3];
UIImage *noRenderinfo = [[UIImage imageNamed:#"info"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoTabIcon setImage:noRenderinfo];
[infoTabIcon setTitle:#"Info"];
and the result was this :
But now with 7.1 and Xcode 5.1 I get this :
I also tried this code :
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
and that looked great on iOS 7.1, but on 7 ended up being a slight brown color.
So how can I make it so it works on both?
Based on Leo Natan's comments I did this :
Edit
I ended up just adding both code snippets together and it works how I want it to
App delegate
{
// iOS 7 method
// Graph tab icon
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *graphTabIcon = [tabBar.items objectAtIndex:0];
UIImage *noRenderGraph = [[UIImage imageNamed:#"graph"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[graphTabIcon setImage:noRenderGraph];
[graphTabIcon setTitle:#"Graph"];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
// Friends tab icon
UITabBarItem *friendsTabIcon = [tabBar.items objectAtIndex:1];
UIImage *noRenderFriends = [[UIImage imageNamed:#"group"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[friendsTabIcon setImage:noRenderFriends];
[friendsTabIcon setTitle:#"Friends"];
// Settings tab icon
UITabBarItem *settingsTabIcon = [tabBar.items objectAtIndex:2];
UIImage *noRenderSettings = [[UIImage imageNamed:#"settings"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[settingsTabIcon setImage:noRenderSettings];
[settingsTabIcon setTitle:#"Settings"];
// Info tab icon
UITabBarItem *infoTabIcon = [tabBar.items objectAtIndex:3];
UIImage *noRenderinfo = [[UIImage imageNamed:#"info"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[infoTabIcon setImage:noRenderinfo];
[infoTabIcon setTitle:#"Info"];
// iOS 7.1 method
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
}

setting image on UITabBar shows an extra line on Image

I am using a UITabBarController in my application (for iPhone 5). When i am trying to set an image on the TabBAr, the image shows a line. I have seen two questions similar to mine, but did not understand the solution.
Here's how I am adding the image:
UIImageView *tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tab_mypeople.png"]];
tabBarView.frame = CGRectMake(0, -15, 320, 64);
[tabBarController.tabBar addSubview:tabBarView];
The dimensions of my current image is 320X64 pixels.
How to resolve this issue??
write this in appdelegate.m didfinishLanching method
UIImage *tabBackground = [[UIImage imageNamed:#"tab_bg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"tab_select_indicator"]];
In iOS 6 and aboove the UITabBar has a shadow image, if you want to disable it you can just call yourTabBar.shadowImage = [UIImage new].
In your case tabBarController.tabBar.shadowImage = [UIImage new]
Yes, it is possible to make it exact size.
Use this code if you are just using a UITabBar inside your view:
Type :1
UITabBar *tabBar = [[UITabBar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 60)];
[self.view addSubview:tabBar];
UIImageView *tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBar.png"]];
tabBar.backgroundImage = tabBarView.image;
Presently my image size is 320 x 5
Type 2:
If you are using a UITabBarController then use it like this
UIImageView *tabBarView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tabBar.png"]];
UITabBarController *tabController = [[UITabBarController alloc]init];
tabController.tabBar.backgroundImage = tabBarView.image;
Here are the screen shot for both.

Unable to make Navigation Bar totally transparent in iOS6

I was using the following code to make my Navigation Bar transparent in iOS5:
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
Upgraded to iOS6 and the Navigation Bar is still transparent but now has a thin black line underneath it. How can I make the Navigation Bar totally transparent?
I have also tried all of the following:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
Thanks in advance.
Solved. iOS6 has added a drop shadow to the Navigation Bar. So the masking code that I was using with iOS5 still works fine - I just need to add
if ([self.navigationController.navigationBar respondsToSelector:#selector(shadowImage)])
{
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
to get rid of the drop shadow.
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
if ([self.navigationController.navigationBar respondsToSelector:#selector(shadowImage)])
{
[self.navigationController.navigationBar setShadowImage:[[[UIImage alloc] init] autorelease]];
// autorelease is necessary, or else [[UIImage alloc] init]'s retainCount is 2.
}

Resources