当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前回答
self.navigationController.navigationBar.backItem.title = @"TEXT";
在Swift中:
self.navigationController?.navigationBar.backItem?.title = "TEXT"
其他回答
我在iOS新,但我会提供我的覆盖导航控制器类的非常简单的答案。 我有简单的覆盖推和弹出方法,并保存以前的视图控制器的标题。抱歉在js block中进行了粘贴。有点困惑如何过去它在正常的代码块。
#import "MyCustomNavController.h" @implementation MyCustomNavController { NSString *_savedTitle; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated withBackBtnTitle:(NSString *)title { _savedTitle = self.topViewController.title; self.topViewController.title = title; [super pushViewController:viewController animated:animated]; } - (UIViewController *)popViewControllerAnimated:(BOOL)animated { [self.viewControllers objectAtIndex:self.viewControllers.count - 2].title = _savedTitle; return [super popViewControllerAnimated:animated]; } @end
我发现,更改返回按钮名称最简单的方法是将视图控制器的标题设置为返回按钮的标题,然后将视图控制器导航项中的titleView替换为带有它的真实名称的自定义标签。
是这样的:
CustomViewController.m
@implementation CustomViewController
- (NSString*)title {
return @"Back Button Title";
}
- (void)viewDidLoad {
[super viewDidLoad];
UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
customTitleView.text = @"Navigation Bar Title";
customTitleView.font = [UIFont boldSystemFontOfSize:20];
customTitleView.backgroundColor = [UIColor clearColor];
customTitleView.textColor = [UIColor whiteColor];
customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
customTitleView.shadowOffset = CGSizeMake(0, -1);
[customTitleView sizeToFit];
self.navigationItem.titleView = [customTitleView autorelease];
}
@end
这将使你的标题在UINavigationBar看起来就像它是本地的。让视图控制器能够拥有分离的标题和返回按钮标题。
在视图控制器A和B的情况下,A负责告诉它的后退按钮应该是什么样子,而B是显示的。
编辑:这也保持了后退按钮的原始外观(左边带箭头的栏按钮项)。
在《ChildVC》中,这对我来说很管用……
self.navigationController.navigationBar.topItem.title = @"Back";
也适用于Swift !
self.navigationController!.navigationBar.topItem!.title = "Back"
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(OnClick_btnBack:)];
self.navigationItem.leftBarButtonItem = btnBack;
[btnBack release];
self.navigationController.navigationBar.backItem.title = @"TEXT";
在Swift中:
self.navigationController?.navigationBar.backItem?.title = "TEXT"