当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前回答
使用下面的代码行:
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"hello"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItems =[[NSArray alloc] initWithObjects:newBackButton, nil];
self.navigationItem.leftItemsSupplementBackButton = YES;
其他回答
我发现,更改返回按钮名称最简单的方法是将视图控制器的标题设置为返回按钮的标题,然后将视图控制器导航项中的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是显示的。
编辑:这也保持了后退按钮的原始外观(左边带箭头的栏按钮项)。
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Log out"
style:UIBarButtonItemStyleDone
target:nil
action:nil] autorelease];
你可以把它放在父控制器代码中的任何你喜欢的地方,这允许你为不同的子视图有不同的后退按钮。
self.navigationController.navigationBar.backItem.title = @"TEXT";
在Swift中:
self.navigationController?.navigationBar.backItem?.title = "TEXT"
这在Swift 4和iOS 11中都适用。最常见的情况是,当用户点击表视图中的一行,一个新的视图控制器被推送到导航堆栈时,我需要这样做。通常我希望返回按钮是被点击的行所表示的对象的名称或某些属性。我通常在didSelectRowAt中这样做。是这样的:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let object = // get the object this row represents
let backButton = UIBarButtonItem()
backButton.title = object.name // or whatever
self.navigationItem.backBarButtonItem = backButton
// now show or segue to the next view controller
}
我在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