How to set image on UITabbaritem? - ios

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

Related

How to set image in uitabbaritem for default

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];

Setting TabBar Item title and Image on Selection in XCode 6.4

I am using xcode 6.4 and using storyboard to set up Tab bar controller Item and its Images.
Issue 1- In storyboard on tab bar item selection, there are already option for Bar Item and Image selection. i have assigned Images on both field but Images are not changing at time of selection.Only default(black) image exists.
I have referred to this Link . Then also default (black) exists
Issue 2- In storyboard i have assigned all images on Bar Item and Image Selection and used given code then title is not visible in tab bar item. only image is working fine on selection.
Code-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Assign tab bar item with titles
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];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = #"Home";
tabBarItem2.title = #"Search";
tabBarItem3.title = #"Cart";
tabBarItem4.title = #"Account";
tabBarItem5.title = #"More";
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"HomeSelected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"SearchOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:#"CartOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem4 setSelectedImage:[[UIImage imageNamed:#"ProfileOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem5 setSelectedImage:[[UIImage imageNamed:#"MoreTabOpened.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// Change the tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.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], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:255.0/255.0 green:109.0/255.0 blue:0.0/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
titleHighlightedColor, NSForegroundColorAttributeName,
nil] forState:UIControlStateHighlighted];
return YES;
}
I've seen gone through iOS Docs, but could not find any useful update on these issues. please update me on it.
You should create your custom tabbar
Useful link : http://swiftiostutorials.com/tutorial-custom-tabbar-storyboard/

how to remove line in UITabBar?

Help remove the line in tab bar!
`[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];` - NOT WORK
CODE:
[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:54.0f/255.0f green:62.0f/255.0f blue:69.0f/255.0f alpha:1.0f]];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
[[self.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:#"barMap.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"barMapNo.png"]];
[[self.tabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#"barNews.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"barNewsNo.png"]];
[[self.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:#"barNew.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"barNewNo.png"]];
If you're trying to remove the shadowImage then just do the same thing you're doing, but assign it to shadowImage instead of backgroundImage.
It looks likes you're setting an image to a tab bar item (with default frame) when the image is larger than the button's frame. You need to replace the entire button!
UIImage *image = [UIImage imageNamed:#"image"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[tabBar setItems:#[ barButton, /* etc */ ]];
Try this,
//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];
// or
// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];
Here is apple guideline for shadow image.
Default is nil. When non-nil, a custom shadow image to show instead of
the default shadow image. For a custom shadow to be shown, a custom
background image must also be set with -setBackgroundImage: (if the
default background image is used, the default shadow image will be
used).

how to remove uibaritem background image in IOS?

Hi i used this code to set the background image for my bar button. I put it in "AppDelegate.h"
//-- set bar button image
UIImage *barButtonImage = [[UIImage imageNamed:#"bar-button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
It worked fine but in my Search Controller I dont want to use it, because it make my "search" button look very ugly. Is there anyway to remove the background image in search controller only ? Plz helpe me. Thanks!
In your SearchController.m try
[[UIBarButtonItem appearance] setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
if you want to remove background image of Search controller
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
and you can add clear background image to it.
UIImageView *searchImage=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 319.0, 44.0)];
[searchImage setImage:[UIImage imageNamed:#"searchbg.png"]];
[searchBar insertSubview:searchImage atIndex:1];
You can set where you want the customization, for instance in tis snippet I only want it in UINavigationBar.
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
Not checked in xcode but the concept is OK. Here the doc + (instancetype)appearanceWhenContainedIn:(Class )ContainerClass,.... Great guide also here: NSHipster.
As you can see is a variadic arguments API, so you can set more classes
PS: I don't know if it is a wanted effect, but the kind of reflection is due to wrong insets in the image.
[searchBarObject setImage:nil forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
This work for me.. I hope it will be helpful for you..
Updated full code..
.m file contents in viewDidLoad
//Table initialization..........................................................................
my_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
//SearchBar initialization......................................................................
UISearchBar *search = [UISearchBar new];
[search sizeToFit];
search.delegate = self;
//Make the image in size 20x20................................................................
UIImage *img = [UIImage imageNamed:#"apple.jpg"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 20), YES, 0);
[img drawInRect:CGRectMake(0, 0, 20, 20)];
UIImage *img2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Set the image for the UISearchbarIconSearch...................................................
[search setImage:img2 forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
search.barStyle = UIBarStyleBlackTranslucent;
[my_table setTableHeaderView:search];
[my_table reloadData];
//SearchDisplay initialization..................................................................
UISearchDisplayController *display = [[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];
sbc = display; //
display.delegate = self;
display.searchResultsDataSource = self;
display.searchResultsDelegate = self;
my_table.dataSource = self;
my_table.delegate = self;
[self.view addSubview:my_table];
.h file.
#interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
{
NSArray *table_array;
UITableView *my_table;
UISearchDisplayController *sbc;
NSArray *filtered;
}
#end
This is my code which change the search icon image...

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