【问题标题】:Add a button next to the back button在后退按钮旁边添加一个按钮
【发布时间】:2013-09-16 15:04:21
【问题描述】:

在iOS中,是否可以在默认后退按钮旁边添加一个按钮(使用pushViewController方法时自动创建的那个)

我尝试使用 setLeftBarButtonItemssetBackBarButtonItem 方法但徒劳无功 - 每当我设置左栏按钮项目时,默认的后退按钮都会消失。

感谢您的帮助!

【问题讨论】:

标签: ios objective-c uinavigationcontroller uinavigationitem


【解决方案1】:

我还没有测试过下面的代码,但只要backBarButtonItem 已经初始化,它就应该可以工作。

[[self navigationItem] setLeftBarButtonItems:[NSArray arrayWithObjects:[[self navigationItem] backBarButtonItem], [[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(action:)], nil]];\

基本上,您是从头开始设置整个左栏按钮项数组,但提供后退按钮以及您自己的自定义按钮。

【讨论】:

  • 感谢您的回答,但它不起作用 - [[self navigationItem] backBarButtonItem]viewWillAppear 中返回 nil
  • @Naz 我的问题中还有setLeftBarButtonItems :-)
【解决方案2】:

做事

self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItems = @[item1, item2];

【讨论】:

  • 这就是我喜欢 SO 的原因。 +1 setLeftBarButtonItems 方法。我会使用titleview 实现同样的目标。感谢您的提示。
  • +1:是的,这就是该属性的用途。来自UINavigationBar.h 标头,By default, the leftItemsSupplementBackButton property is NO. In this case, the back button is not drawn and the left item or items replace it. If you would like the left items to appear in addition to the back button (as opposed to instead of it) set leftItemsSupplementBackButton to YES.
  • +1 表示leftItemsSupplementBackButton。我以前没见过这种方法。好电话。
  • 完美!不错,不是黑客!
  • 有没有办法在 AppDelegate 中全局应用这个?