普通样式的 UINavigationBar BarButtonItem

发布于 2024-08-23 08:28:54 字数 603 浏览 2 评论 0原文

我得到以下代码:

- (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 的按钮 替代文本

有没有办法在这个位置设置一个普通样式的按钮?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

故人爱我别走 2024-08-30 08:28:55
    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;
    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;
少女情怀诗 2024-08-30 08:28:55

试试这个,

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didPressButton)];

Try this,

UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(didPressButton)];
浪漫人生路 2024-08-30 08:28:54

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

IBOutlet UIButton * _myButton;

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

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

这有效,因为 UIButton 是 UIView 的子类。

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.

污味仙女 2024-08-30 08:28:54

试试这个:

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

希望有帮助。

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.

初见 2024-08-30 08:28:54

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

结果:

在此处输入图像描述

当使用此图标图像时(它是白色的)背景所以在这里不可见 - 链接是: https://i.sstatic.net/lk5SB.png< /a>):
在此处输入图像描述

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: https://i.sstatic.net/lk5SB.png):
enter image description here

又怨 2024-08-30 08:28:54

这很奇怪,但确实有效。对图像/渠道说“不”!您甚至不应该设置 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];

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];
开始看清了 2024-08-30 08:28:54

虽然 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 有一个解决方案

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.

你是年少的欢喜 2024-08-30 08:28:54

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文