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条回答
仙女界的扛把子
2楼-- · 2019-01-22 13:38

Why not try UI7Kit? Works well for me, easy to use.

查看更多
爷的心禁止访问
3楼-- · 2019-01-22 13:38
    UIImage *img = [UIImage imageNamed:@"white_star.png"];

      CGSize itemSize = CGSizeMake(20, 20);
      UIGraphicsBeginImageContext(itemSize);
      CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
      [img drawInRect:imageRect];
      img = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();


  UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:@selector(favoriteButtonClicked)];
  barButton.style = UIBarButtonItemStyleBordered;
查看更多
Bombasti
4楼-- · 2019-01-22 13:41

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.

查看更多
狗以群分
5楼-- · 2019-01-22 13:41

It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
                    initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];

item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
查看更多
该账号已被封号
6楼-- · 2019-01-22 13:45

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.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-22 13:50

Try this,

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didPressButton)];
查看更多
登录 后发表回答