【问题标题】:Draw custom Back button on iPhone Navigation Bar在 iPhone 导航栏上绘制自定义后退按钮
【发布时间】:2011-05-14 16:47:02
【问题描述】:

我有一个带有 NavigationBar 的 UIView,我想添加一个看起来像后退按钮样式的按钮。我没有使用 UINavigationController 并且想知道如果没有它是否可以完成?

按钮的样式应该是这样的:

谢谢

【问题讨论】:

  • @casperOne 除了在回答近 2 年后关闭一个问题,你为什么要关闭这个? stackoverflow.com/questions/227078 中接受的答案建议使用图像,而 Mike Weller 下面的答案要优雅得多。更不用说,这里的答案将支持更新的设备(例如,我没有在启动视网膜显示器时更改此代码示例,它仍然可以工作。图像将显示低分辨率完成)。

标签: iphone objective-c uinavigationcontroller uinavigationbar back-button


【解决方案1】:

您需要设置一个自定义堆栈UINavigationItem 对象并将它们推送到 UINavigationBar。这是我所知道的获得真正后退按钮的唯一方法。我没有测试过这段代码,但你应该这样做:

UINavigationItem *previousItem =
    [[[UINavigationItem alloc] initWithTitle:@"Back title"] autorelease];

UINavigationItem *currentItem =
    [[[UINavigationItem alloc] initWithTitle:@"Main Title"] autorelease];

[navigationBar setItems:[NSArray arrayWithObjects:previousItem, currentItem, nil]
               animated:YES];

要在按下按钮时进行处理,您应该将自己设置为导航栏的委托并实现 UINavigationBarDelegate 委托。

【讨论】:

  • 仅供参考:设置由 navigationController 管理的 navigationBar 的委托会引发异常:*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cannot manually set the delegate on a UINavigationBar managed by a controller.'
  • 当前项目是什么?
【解决方案2】:

您也可以通过修改前一个视图控制器(不是当前查看的)上的backBarButtonItem 来更新它。

【讨论】:

  • 嗨,我认为你的回答可以帮助我解决我的问题,请如何在我的代码中使用 backBarButtonItem,假设这是我在上一个视图中的代码:-(IBAction)goToRechercherView { rechercherViewController. modalTransitionStyle=UIModalTransitionStyleFlipHorizo​​ntal; [自我presentModalViewController:rechercherViewController动画:YES]; }
猜你喜欢
  • 2012-01-03
  • 2016-04-01
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
相关资源
最近更新 更多