0

I want to create a system UIBarButton, but I want it to have plain style.
I've tried with this code, but the style is ignored. What's wrong with it?

UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearch)];    
search.style = UIBarButtonItemStylePlain;
self.navigationItem.rightBarButtonItem = search;
[search release];
pasine
  • 11,311
  • 10
  • 49
  • 81
  • `search.style = UIBarButtonItemStylePlain;` is unnecessary as `UIBarButtonItemStylePlain` is the default value for the style property of this Class. From [UIBarButtonItem_Class](http://developer.apple.com/library/ios/documentation/uikit/reference/UIBarButtonItem_Class/UIBarButtonItem_Class.pdf). – chown Sep 08 '11 at 16:30
  • I add it to try to remove the borders. Documentation says plain style is the default, but that's not true in my case. The system button is bordered. – pasine Sep 08 '11 at 23:51

2 Answers2

2

The main thing here is that you need to place your button in the previous controller, not in the one that is going to be pushed.

//Your actual ViewController
UIBarButtonItem *search = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(showSearch)];    
search.style = UIBarButtonItemStylePlain;
self.navigationItem.rightBarButtonItem = search;
[search release];

//Controller that is going to be pushed and will display the new UIBarButtonItem
[self.navigationController pushViewController:newViewController animated:YES];
Ecarrion
  • 4,940
  • 1
  • 33
  • 44
0

Found a similar thread here.

It seems that the way to go about making this plan is to create your custom UIButton and assign it to the bar button.

Community
  • 1
  • 1
Madhu
  • 2,429
  • 16
  • 31
  • 1
    Using UIButton I lose the advantage of system button that is avoid to create my own image. – pasine Sep 08 '11 at 23:49
  • I understand. I can't verify this without trying this out on code, but I'm wondering whether that could be the issue here -> Perhaps the system buttons come with their own border style by default. Will look into it if I get some time. – Madhu Sep 09 '11 at 00:07
  • I ended up creating an image. Not what I was looking for, but it seems the only solution. – pasine Sep 19 '11 at 21:00
  • I've been looking for a solution to this myself... Don't want to re-create the standard button images. Nothing yet... – Stian Høiland Mar 12 '12 at 19:54