Jacob Wood
Jacob Wood

Reputation: 467

bar button item image is turning to white icon even if its black

I am using these icons as bar button item in a toolbar
http://stopiransnukes.org/images/home_icon.png
and
http://www.bladmuziekplus.nl/shop/files/images/icon_home.png
But background image is converted to white automatically even if its black.

I tried setting image background via code but still its turning into white.

Manually : I have Dragged+Dropped the above image to the project.I have selected the bar button item and on 4th option on the right bar (shield icon) select different tint and under bar button drop-down image is set to the above image.

Programmatically :

  [self.homeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];

   [[self.homeButton] setImage:[UIImage imageNamed:@"home_icon.png"]];

How do I fix it ?
Thanks in Advance.

.

Upvotes: 3

Views: 4526

Answers (4)

Dinesh Raja
Dinesh Raja

Reputation: 8501

This is a step by step process to add a image into UIButton and add that UIButton as a custom view inside the UIBarButtonItem in the storyboard itself.. I will add some images for you to understand this totally..

step 1: Drag and drop the toolbar in storyboard view Drag and drop the toolbar in your view

step 2: Drag and drop the UIButton in your view

enter image description here

step 3: Drag and drop the image in your UIButton

enter image description here

step 4: Drag and drop the UIButton in the toolbar and now you can in the right side that it is added inside of the UIBarButtonItem. now you just have to connect an action method for your button.

enter image description here

Upvotes: 2

Güngör Basa
Güngör Basa

Reputation: 628

viewDidLoad

[super viewDidLoad];

//uıbarButtonItem firstButtonItem,secondButtonItem
UIImage* firstButtonImage = [UIImage imageNamed:@"firstImageName"];;
CGRect frameimg = CGRectMake(0, 0, 30, 30);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:firstButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(goToUserProfile)
     forControlEvents:UIControlEventTouchUpInside];
self.firstButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];


UIImage* secondButtonImage = [UIImage imageNamed:@"secondImageName"];
frameimg = CGRectMake(0, 0, 30, 30);
someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:secondButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(likePhoto)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setTag:1];
self.secondButtonItem = [[UIBarButtonItem alloc] initWithCustomView:someButton];

[self.toolbar setItems:[self addItemsToToolbar]];

AddItemsToToolbar Method

- (NSMutableArray *)addItemsToToolbar{
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[items addObject:flexSpace];
[items addObject:self.firstButtonItem];
[items addObject:flexSpace];
[items addObject:self.secondButtonItem];
return items;

}

Upvotes: 1

Güngör Basa
Güngör Basa

Reputation: 628

UIImage* infoButtonImage = [UIImage imageNamed:@"info_button.png"];;
CGRect frameimg = CGRectMake(0, 0, infoButtonImage.size.width, infoButtonImage.size.height);
UIButton * someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:infoButtonImage forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(actionOfButton)
     forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * infoButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];

This code is going to solve your problem

Upvotes: 0

artey
artey

Reputation: 1243

This is the normal behaviour, you can read about it in the Human Interface Guidelines.

http://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/IconsImages/IconsImages.html#//apple_ref/doc/uid/TP40006556-CH14-SW8

If you want to change that you need to write your own subclasses.

Upvotes: 1

Related Questions