当我将一个新的ViewController推入导航控制器栈时,“返回”按钮是上一个控制器的标题。
如何将返回按钮中的文本从默认的“上一个控制器名称”更改为“返回”?
当我将一个新的ViewController推入导航控制器栈时,“返回”按钮是上一个控制器的标题。
如何将返回按钮中的文本从默认的“上一个控制器名称”更改为“返回”?
这是使用除了前一页标题之外的其他东西创建返回按钮的正确方式
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"Your Title";
self.navigationItem.backBarButtonItem = barButton;
据我所知:
self.navigationItem.backBarButtonItem.title = @"Your Title";
会将上一页导航栏的标题设置为此内容,这不是您想要的。
我知道,这个问题很老了,但我找到了一个好的解决方案。
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] init];
barButton.title = @"back";
self.navigationController.navigationBar.topItem.backBarButtonItem = barButton;
来自childView的作品!已在iOS 7上进行测试。
你需要在导航控制器上创建一个自定义按钮。将以下代码放入根视图控制器的viewDidLoad中:
UIBarButtonItem * tempButtonItem = [[[ UIBarButtonItem alloc] init] autorelease];
tempButtonItem .title = @"Back";
self.navigationItem.backBarButtonItem = tempButtonItem ;
通过在根视图控制器上设置导航栏按钮,推入的视图会显示相应的返回按钮。
self.title = @"Custom Title";
[self pushViewController: newViewController ...];
and in the navigation controller's delegate class
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
if([viewController class] == [OldViewController class]) {
viewController.title = @"Your previous title";
}
}
你可以在主视图控制器的navigationItem
的标题上设置标题。基本上,每个UIViewController
都有一个小存根UINavigationItem
,其中包含有关如何在UINavigationController
中引用该视图的元数据。默认情况下,该元数据会回退到UIViewController
本身。
假设“self”是在UINavigationController
中可见的视图的UIViewController
,请设置:
self.navigationItem.title = @"My Custom Title"
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for this view
self.navigationItem.title = "My Title"
}
override func viewWillDisappear(animated: Bool) {
super.viewWillAppear(animated)
//Set Title for back button in next view
self.navigationItem.title = "Back"
}