2

I want to set image in bar button item like below.

enter image description here

I added this image to Assets.xcassets and when i tried to set image, it changes color automatically.

enter image description here

how can I set image as it is in bar button item?

Divyesh
  • 19
  • 7

4 Answers4

1

Well, you have to edit your image first. Make it transparent (PNG) if it's not, and objects must be be white within the image, in your case it's black now. Then in your code change the tint color like this:

let myImage = UIImage(named: "myImage")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myImage.tintColor = UIColor.blackColor()
Maysam
  • 7,246
  • 13
  • 68
  • 106
1

I think you need to change the rendering mode of image to always original. It is taking template image of your image.

let img:UIImage = UIImage(named: "Bitcoin")!
img.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

try changing the rendering mode of the image before setting it to bar button. for more info on Image Rendering Mode refer this

for Objective C....

UIImage *img = [UIImage imageNamed:@"Bitcoin"];
UIImage *original = [img imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];

:)

Bhaumik Desai
  • 213
  • 1
  • 9
0

Try to change the Tint colour. The tint default colour is blue - try to change it

Gal Marom
  • 8,499
  • 1
  • 17
  • 19
0
UIImage* image3 = [UIImage imageNamed:@"search_button.png"];
CGRect frameimg = CGRectMake(15,5, 25,25);

UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(Search_btn:)
     forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *searchButton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.leftBarButtonItem =mailbutton;
Piyush
  • 1,534
  • 12
  • 32