当我创建自定义后退按钮时,我使用以下代码:
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Yeah" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonPressed:)];
self.navigationItem.leftBarButtonItem = leftButton;
这很好用,我得到了这个结果:
我会得到相同的结果,但是左边有一个箭头,就像这样(当它是一个标准的后退按钮,而不是一个自定义的按钮时):
如何简单地添加此箭头?
答案 0 :(得分:30)
最后,这是我用来定义后退按钮标题的片段,标题左箭头在当前视图中,而不是在父视图中:
- (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上添加操作,因此如果需要,可以参考this great post。
针对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
答案 1 :(得分:17)
最简单的方法是在父控制器中设置标题(即您要导航回的控制器)。如果您不希望它与该VC视图中显示的实际标题相同,您可以将viewWillDisappear中的标题更改为您在下一个VC的后退按钮上所需的标题,然后将其更改回您想要的内容。 viewWillAppear中的父级。
如果您使用的是故事板,您也可以直接在IB中设置后面的标题。
最后,为了创建自定义后退按钮,您可以执行以下操作:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Details" style:UIBarButtonItemStyleBordered target:nil action:nil];
...确保在展示(或父级)视图控制器中执行此操作,而不是正在加载的视图控制器(显示的控制器)。
答案 2 :(得分: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;
答案 3 :(得分: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];
答案 4 :(得分:0)
我使用的代码如下:
UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] init];
leftBar.title = @"Back";//Details
self.navigationController.navigationBar.topItem.backBarButtonItem = leftBar;
答案 5 :(得分: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
}
}
这应该从父viewController调用,例如在viewWillDisappear -
中override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.setBackButtonTitle(to: "Back"))
}