28

创建自定义后退按钮时,我使用以下代码:

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Yeah" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonPressed:)];
self.navigationItem.leftBarButtonItem = leftButton;

这很好用,我得到了这个结果:

后退按钮通常位于的“详细信息”一词的屏幕截图。

我会得到相同的结果,但左侧有一个箭头,如下所示(当它是标准后退按钮,而不是自定义按钮时):

标准后退箭头的屏幕截图,后跟“详细信息”一词。

我怎样才能简单地添加这个箭头?

4

6 回答 6

29

最后,这是我用来在当前视图中使用标准左箭头定义后退按钮标题的片段,而不是在父视图中

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setTitle:@"Current View"];

    // Get the previous view controller
    UIViewController *previousVC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 2];

    // Create a UIBarButtonItem
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"FooBar" style:UIBarButtonItemStyleBordered target:self action:@selector(yourSelector)];

    // Associate the barButtonItem to the previous view
    [previousVC.navigationItem setBackBarButtonItem:barButtonItem];
}

结果如下:

在此处输入图像描述

注意:但是,由于无法在 backBarButtonItem 上添加操作,因此如果您愿意,可以参考这篇很棒的帖子。

为 Swift 更新

// Prev - no chevron...
//navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Back !", style: .plain, target: self, action: #selector(backPressed))

// adds the chevron
let vc = navigationController?.viewControllers.first
let button = UIBarButtonItem(title: "Go Back", style: .plain, target: self, action: #selector(backPressed))
vc?.navigationItem.backBarButtonItem = button
于 2014-11-25T13:04:20.113 回答
17

最简单的做法是在父控制器中设置标题(即您想要导航回的那个)。如果您不希望这与该 VC 视图中显示的实际标题相同,您可以将 viewWillDisappear 中的标题更改为下一个 VC 的后退按钮上所需的标题,然后将其更改回您想要的父视图将出现。

如果你使用storyboards,你也可以直接在IB中设置back title。

最后,为了创建自定义后退按钮,您可以执行以下操作:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Details" style:UIBarButtonItemStyleBordered target:nil action:nil];

...只需确保在呈现(或父)视图控制器中执行此操作,而不是正在加载的视图控制器(呈现的控制器)。

于 2013-10-31T13:38:28.870 回答
3
UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:FRAME_DEFINE
[backButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[backButton setExclusiveTouch:YES];
[backButton setImage:[UIImage imageNamed:BACK_BUTTON_DEFAULT_ICON] forState:UIControlStateNormal];
[backButton setTitle:@"BACK" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
UIBarButtonItem *backMenuBarButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backMenuBarButton;
于 2013-10-31T13:47:16.550 回答
0

好吧,您需要使用背景图像和标题

// Creates a back button instead of default behaviour (displaying title of previous screen)
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_arrow.png"]
                                                               style:UIBarButtonItemStyleBordered
                                                              target:self
                                                              action:@selector(backAction)];

tipsDetailViewController.navigationItem.leftBarButtonItem = backButton;
[backButton release];
于 2013-10-31T13:37:07.003 回答
0

我使用如下代码:

UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] init];
leftBar.title = @"Back";//Details
self.navigationController.navigationBar.topItem.backBarButtonItem = leftBar;
于 2017-01-27T11:20:32.727 回答
0

对于 Swift,以 Erzekiel 的回答为基础,您可以将其简化为 -

extension UIViewController {

    func setBackButtonTitle(to title: String) {
        let barButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
        self.navigationItem.backBarButtonItem = barButtonItem
    }
}

这应该从父视图控制器调用,例如在 viewWillDisappear -

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.setBackButtonTitle(to: "Back"))
}
于 2018-01-23T14:45:24.120 回答