views:

1162

answers:

4

Hi,

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?

A: 

Override viewDidLoad instead:

-(void)viewDidLoad {
  [super viewDidLoad];
  self.title = @"please wait";
  UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"star.png"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonFavoriteClicked:)];
  self.navigationItem.rightBarButtonItem = favorite;
}
Frank Shearar
has no effect,.. looks the same
choise
+1  A: 

Although Frank is right, this code should be in viewDidLoad, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.

-(void)viewDidLoad {
  [super viewDidLoad];
  self.title = @"please wait";
  self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}

Edit:

Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.

Seems this isn't currently possible.
sbwoodside has a solution.

David Kanarek
also, rightBarButtonItems need a UIBarButtonItem as Object Type
choise
Really? Damn, I know you can stick whatever you want in the title. That's really disappointing.
David Kanarek
`@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;` hm, or do i interpret something wrong?
choise
Nope, you're definitely right, I just never noticed as I always use the bordered button.
David Kanarek
so its not possible :(
choise
it is possible, see below.
sbwoodside
+3  A: 

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.

Juan
+2  A: 

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.

sbwoodside