xcode 如何手动向 UINavigationBar 添加“后退”按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5227200/
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
How do i add a 'back' button to a UINavigationBar manually?
提问by Andrew
I want a navigationbar to have a back button which goes to the last page, even though the last page didn't have a navigation bar. How can i insert a back button, and control what happens when the user presses it?
我想要一个导航栏有一个返回最后一页的按钮,即使最后一页没有导航栏。如何插入后退按钮,并控制用户按下后会发生什么?
回答by Josh Wolff
I would like to add an answer, but unfortunately I only have the correct answer in Swift. I will provide it here for anyone who comes along this question looking for an answer in Swift. Let's suppose you would like to add a "Back Button" to your navigation bar to go to the last page. Do the following:
我想添加一个答案,但不幸的是我只有 Swift 中的正确答案。我会在这里为任何遇到这个问题并在 Swift 中寻找答案的人提供它。假设您想在导航栏中添加一个“后退按钮”以转到最后一页。请执行下列操作:
let button = UIBarButtonItem.init()
button.title = "BACK"
let attributes : [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue): UIFont.systemFont(ofSize: 15.0)]
button.setTitleTextAttributes(attributes, for: .normal)
button.target = self
button.action = #selector(self.goBack)
self.navigationItem.setLeftBarButton(button, animated: false)
Then, define a function (that is used for the selector for the button's action above):
然后,定义一个函数(用于上面按钮操作的选择器):
@objc func goBack() {
self.performSegue(withIdentifier: "myUnwindSegue", sender: AnyObject.self)
}
Note that here I make use of an unwind segue. To use an unwind segue, please refer to hereor comment below if you have any questions. Unwind segues can be tricky when you first use them, but afterwards they are great. Also note that when you say "last page" I assume you mean a view already added to the view hierarchy.
请注意,这里我使用了 unwind segue。要使用 unwind segue,如果您有任何问题,请参阅此处或在下面发表评论。当您第一次使用它们时,放松 segue 可能会很棘手,但之后它们就很棒了。另请注意,当您说“最后一页”时,我假设您的意思是已添加到视图层次结构中的视图。
回答by florian
You need to link the button from Interface Builder to the method you want to be called when the button has been clicked on. The method should look like this - (IBAction)backAction
. The name of the method is up to you but you have to declare the return type to IBAction so Interface builder is aware that a button can be linked to it.
您需要将 Interface Builder 中的按钮链接到单击按钮时要调用的方法。该方法应如下所示- (IBAction)backAction
。方法的名称由您决定,但您必须将返回类型声明为 IBAction,以便接口构建器知道可以将按钮链接到它。
回答by Caleb
A navigation bar isn't required for a view controller to be managed by a navigation controller. Rather than trying to fake a back button, use a navigation controller to manage both view controllers, and have the first controller hide the navigation bar. For example, you can add something like:
由导航控制器管理的视图控制器不需要导航栏。与其尝试伪造后退按钮,不如使用导航控制器来管理两个视图控制器,并让第一个控制器隐藏导航栏。例如,您可以添加如下内容:
[self.navigationController setNavigationBarHidden:YES animated:YES];
to the controller's -viewDidAppear method. Do something similar for the second controller, passing in NO for the hidden parameter in order to display it again.
到控制器的 -viewDidAppear 方法。对第二个控制器做类似的事情,为隐藏参数传入 NO 以再次显示它。
In the general case, though, a view controller can add a back button to the nav bar with code like this (warning: untested code typed from memory, but should get you started):
但是,在一般情况下,视图控制器可以使用如下代码向导航栏添加后退按钮(警告:从内存中键入未经测试的代码,但应该可以帮助您入门):
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:myBackImage style: UIBarButtonItemStylePlain target:self action:someAction];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
When the button is tapped, the button will send its action (someAction) to its target (self).
当按钮被点击时,按钮将它的动作(someAction)发送到它的目标(self)。
回答by Abhisek Mallik
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:11.0]];
[button setBackgroundImage:[UIImage imageNamed:@"back_norm.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"back_click.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:@"back_click.png"] forState:UIControlStateHighlighted];
[button setTitle:@" Back" forState:UIControlStateNormal];
[button setTitle:@" Back" forState:UIControlStateSelected];
[button setTitle:@" Back" forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, 48, 30);
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = back;
[button release];
[back release];