20

我得到以下代码:

- (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;
}

但我的按钮看起来仍然像一个带有 UIBarButtonItemStyleBordered 的按钮 替代文字

有没有办法在这个位置设置一个简单风格的按钮?

4

8 回答 8

23

试试这个:

- (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;
}

希望能帮助到你。

于 2010-03-26T19:57:52.613 回答
23

在界面生成器中创建一个 UIButton,使其看起来完全符合您的要求。在 .h 中为 in 创建一个出口:

IBOutlet UIButton * _myButton;

然后使用自定义视图将其设置为右栏按钮项,这将消除边框:

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

这是因为 UIButton 是 UIView 的子类。

于 2010-05-24T06:25:00.353 回答
8

或者在没有 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;

结果是:

在此处输入图像描述

使用此图标图像时(它是白色背景上的白色,因此在此处不可见 - 链接为:http: //i.stack.imgur.com/lk5SB.png): 在此处输入图像描述

于 2011-11-21T16:50:30.473 回答
4

这很奇怪,但它有效。对图片/网点说“不”!您甚至不应该设置 UIBarButtonItemStylePlain 属性。诀窍是将按钮放入具有一些特殊属性的 UIToolBar 中:

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];
于 2013-02-10T14:39:16.403 回答
1

虽然 Frank 是对的,但这段代码应该在 中viewDidLoad,这里的问题是 BarButtons 的样子。如果你想让它看起来不一样,你需要使用不同类型的 UIView。您可以将 UIButton 添加到 UIToolbar 就好了。

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

编辑:

抱歉,你想要一个普通的 UIBarButtonItem。我不相信你可以用 UINavigationBar 做到这一点。您可以尝试添加一个 UserInteractionEnabled UILabel 作为 rightBarButtonItem,我不确定它是否会起作用。

看来这目前是不可能的。
sbwoodside 有一个解决方案

于 2010-02-27T18:39:59.600 回答
0

为什么不试试 UI7Kit?对我来说效果很好,易于使用。

于 2013-11-25T18:33:42.347 回答
-1
    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;
于 2011-08-22T09:20:18.450 回答
-1

试试这个,

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didPressButton)];
于 2013-02-09T03:48:01.747 回答