0

i want to use the default bar button items like

UIBarButtonItem *editFavoritesButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(startEndEditing:)];

The problem is that i want to change the background of these button but

[editFavoritesButton setBackgroundImage:[UIImage imageNamed:@"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

does not to the trick in this case.

UPDATED:

The idea is not creating a custom button since you need to handle the translations for this case.On the other hand i want to user compose and other sort of default images.

Ilker Baltaci
  • 11,644
  • 6
  • 63
  • 79

5 Answers5

1

Replace your code from...

[editFavoritesButton setBackgroundImage:[UIImage imageNamed:@"barbuttonPlain.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

as like this...

[editFavoritesButton setImage:[UIImage imageNamed:@"barbuttonPlain.png"]];
Bhanu Prakash
  • 1,493
  • 8
  • 20
0

Use this code to create custom UIBarButtonItem:

UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[mybutton addTarget:self action:@selector(mybuttonFunction) forControlEvents:UIControlEventTouchUpInside];
[mybutton setImage:[UIImage imageNamed:@"myImage.png"] forState:UIControlStateNormal];

UIBarButtonItem *random = [[UIBarButtonItem alloc] initWithCustomView:mybutton];

Hope this will help you.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
0

You can try like this If you have any question you can ask..

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 83, 44);
backButton.adjustsImageWhenHighlighted=NO;
[backButton addTarget:self action:@selector(backMethod)
       forControlEvents:UIControlEventTouchDown];
[backButton setImage:[UIImage imageNamed:@"img.png"]
              forState:UIControlStateNormal];


UIBarButtonItem *barLeftInfoButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barLeftInfoButton;
JPiOS
  • 86
  • 3
0

Add a custom button first and then add it to the UIBarButtonItem.

UIImage *buttonImage = [UIImage imageNamed:@"mainButton.png"];

    //create the button and assign the image
self.yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.yourButton setFrame:CGRectMake(5,7,buttonImage.size.width,buttonImage.size.height)];

[self.yourButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
self.yourButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[self.yourButton setTitle:@"title" forState:UIControlStateNormal];
[self.yourButton addTarget:self action:@selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];

    // add image view
[self.navBar addSubview:self.yourButton];


    //create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.yourButton];
self.navigationItem.leftBarButtonItem = customBarItem;
nsgulliver
  • 12,655
  • 23
  • 43
  • 64
0

Please try to use this one ....

UIButton *editFavoritesButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 55.0f, 30.0f)];
[editFavoritesButton setBackgroundColor:[UIColor clearColor]];
[editFavoritesButton setBackgroundImage:[UIImage imageNamed:@"barbuttonPlain.png"] forState:UIControlStateNormal];
[editFavoritesButton addTarget:self action:@selector(startEndEditing:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:editFavoritesButton];
[self.navigationItem setLeftBarButtonItem:leftBtn];
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66