简体   繁体   English

如何从导航栏中删除后退按钮

[英]How to remove back button from navigation bar

I have that code. 我有那个代码。 It can add edit button to navigatin bar, but back button still exists, but it becomes unresponsible. 它可以将编辑按钮添加到导航栏,但后退按钮仍然存在,但不负责任。

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] 
                               initWithTitle:@"Edit"                                            
                               style:UIBarButtonItemStyleBordered 
                               target:nil 
                               action:nil];
[[self.navigationController.navigationBar.items objectAtIndex:1] setRightBarButtonItem:editButton];

[[self.navigationController.navigationBar.items objectAtIndex:1]setHidesBackButton:YES];

simply and short: 简单而简短:

  self.navigationItem.backBarButtonItem = nil;

or 要么

  self.navigationItem.leftBarButtonItem = nil;

I have done something similar to this before. 我以前做过类似的事情。 In the controller that's going to push the next view controller that you don't want a back button in put this wherever you're doing your pushViewController: 在要推送下一个视图控制器的控制器中,无论您在执行pushViewController的哪个位置,都不要将其后退按钮放进去:

 myNextViewController.navigationItem.hidesBackButton = YES;
 [self.navigationController pushViewController:myNextViewController animated:YES];

You can also use 您也可以使用

Objective-C: 目标C:

self.navigationItem.hidesBackButton = YES;

Swift: 迅速:

navigationItem.hidesBackButton = true

Either of these will work 这些都可以工作

self.navigationItem.leftBarButtonItem = nil;

or 要么

self.navigationItem.backBarButtonItem = nil;

This is old but after reading this today the answer of: 这是旧的,但今天读完此答案后:

self.navigationItem.hidesBackButton = YES;

is correct but is incomplete, this needs to be done at a later life cycle method then viewDidLoad, aka toss it in viewWillApper or something later and it will work 是正确的但不完整,这需要在以后的生命周期方法中完成,然后再执行viewDidLoad,也可以在viewWillApper或以后使用它,然后它将起作用

- (void)viewWillAppear:(BOOL)animated{
    self.navigationItem.hidesBackButton = YES;
}

Hope this helps someone looking at an old post as I did today 希望这能像我今天一样帮助某人查看旧帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM