ios 使用左侧的标准箭头在 UINavigationBar 上创建自定义左后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19707833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create a custom left back button on UINavigationBar WITH the standard arrow on the left
提问by Erzékiel
When I create a custom back button, I use the following code:
创建自定义后退按钮时,我使用以下代码:
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Yeah" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonPressed:)];
self.navigationItem.leftBarButtonItem = leftButton;
This works fine, and I obtain this result:
这很好用,我得到了这个结果:
I would have the same result, but with an arrow on the left, like this (when it's a standard back button, not a custom one):
我会得到相同的结果,但左边有一个箭头,就像这样(当它是标准的后退按钮,而不是自定义按钮时):
How can I simply add this arrow ?
我怎样才能简单地添加这个箭头?
回答by Erzékiel
Finally, here's the snippet I use to define the back button's title with the standard left arrow in the current view, not in the parent view:
最后,这是我用来在当前视图而不是父视图中使用标准左箭头定义后退按钮标题的代码段:
- (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];
}
Here's the result :
结果如下:
Note :However, since it's not possible to add an action on a backBarButtonItem, you can refer to this great postif you want it to.
注意:但是,由于无法在 backBarButtonItem 上添加操作,如果您愿意,可以参考这篇很棒的文章。
Updated for Swift
为 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
回答by Bladebunny
The easiest thing to do would be to set the title, in the parent controller (i.e. the one you want to nav back to). If you don't want this to be the same as the actual title displayed in that VC's view, you can change the title in viewWillDisappear to what you want on the next VC's back button, and then change it back to what you want in the parent in viewWillAppear.
最简单的方法是在父控制器中设置标题(即您要导航回的那个)。如果您不希望这与该 VC 视图中显示的实际标题相同,您可以将 viewWillDisappear 中的标题更改为您在下一个 VC 的后退按钮上想要的标题,然后将其更改回您想要的viewWillAppear 中的父级。
If you are using storyboards, you can also set the back title directly in IB.
如果你使用storyboards,你也可以直接在IB中设置back title。
Finally, in order to create a custom back button, you can do something like:
最后,为了创建自定义后退按钮,您可以执行以下操作:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Details" style:UIBarButtonItemStyleBordered target:nil action:nil];
...just be sure to do this in the presenting (or parent) view controller, not the view controller being loaded (the presented controller).
...只是确保在呈现(或父)视图控制器中执行此操作,而不是正在加载的视图控制器(呈现的控制器)。
回答by Durican Radu
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;
回答by Retro
well you need to go with a background image and with title and
好吧,您需要使用背景图像和标题以及
// 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];
回答by Mannam Brahmam
I use code like:
我使用如下代码:
UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] init];
leftBar.title = @"Back";//Details
self.navigationController.navigationBar.topItem.backBarButtonItem = leftBar;
回答by SomaMan
For Swift, using Erzekiel's answer as the basis for this, you can simplify it to -
对于 Swift,使用 Erzekiel 的答案作为此基础,您可以将其简化为 -
extension UIViewController {
func setBackButtonTitle(to title: String) {
let barButtonItem = UIBarButtonItem(title: title, style: .plain, target: self, action: nil)
self.navigationItem.backBarButtonItem = barButtonItem
}
}
This should be called from the parent viewController, eg in viewWillDisappear -
这应该从父视图控制器调用,例如在 viewWillDisappear -
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.setBackButtonTitle(to: "Back"))
}