Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationBar BarButtonItem with Plain Style

Tags:

i got the following code:

- (id)init {
  if (self = [super init]) {
      self.title = @"please wait";
      UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFavoriteClicked:)];
      self.navigationItem.rightBarButtonItem = favorite;
  }

  return self;
}

but my button looks still like a Button with UIBarButtonItemStyleBordered alt text

is there a way to set a button with plain style at this position?

like image 964
choise Avatar asked Feb 27 '10 18:02

choise


2 Answers

Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:

IBOutlet UIButton * _myButton;

Then set it as your right bar button item using a custom view, which will eliminate the border:

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];

This works because UIButton is a subclass of UIView.

like image 139
Simon Woodside Avatar answered Nov 28 '22 23:11

Simon Woodside


Try this instead:

- (id)init {
  if (self = [super init]) {
      self.title = @"please wait";

      UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
      [button setImage:[UIImage imageNamed:@"star.png"] forState:UIControlStateNormal];
      [button addTarget:self action:@selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
      UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
      [button release];

      self.navigationItem.rightBarButtonItem = favorite;
  }

  return self;
}

Hope it helps.

like image 44
Juan Avatar answered Nov 28 '22 22:11

Juan