3
votes

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.

.

4
you need to post your code or screenshot for how you are adding a buttons in your toolbarDinesh Raja
@R.A I have edited the question.Jacob Wood
Why dont you try this. just drag and drop a uibutton and add a image to it in the storyboard itself and add that to uibarbuttonitem by just drag and drop itself and adding a action method to the uibutton makes what u need..try it in storyboard itself. no need of any code except an action method for a buttonDinesh Raja

4 Answers

2
votes

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

1
votes

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.

1
votes

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;

}

0
votes
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