UINavigationBar BarButtonItem with Plain Style

2019-01-22 13:02发布

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 http://img693.imageshack.us/img693/1632/bildschirmfoto20100227ui.png

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

8条回答
可以哭但决不认输i
2楼-- · 2019-01-22 13:53

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.

查看更多
We Are One
3楼-- · 2019-01-22 13:54

Or do this in code without IB:

// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:@"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:@selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;

Results in:

enter image description here

When using this icon image (it's white on a white background so isn't visible here - link is: http://i.stack.imgur.com/lk5SB.png): enter image description here

查看更多
登录 后发表回答