当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前回答
这段代码也可以工作。把这个放到导航控制器的根控制器上:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
其他回答
这里解释的解决方案没有一个对我有效。所以我所做的就是从我来自的场景中删除标题,方法如下:
自我。标题= @"";
因此,当新场景呈现时,后面的文本不会出现。
我完全同意,这不是一个明确的解决方案,但工作,没有一个解释对我有效。
根据UINavigationBar>backItem的文档
如果最上面导航项的leftBarButtonItem属性为 Nil,导航栏显示一个返回按钮,该按钮的标题是派生的 属性中的项。
但是设置backItem。backBarButtonItem在第一次viewWillAppear时不起作用。设置topItem。backBarButtonItem只适用于第一次viewWillAppear。因为navigationBar。topItem仍然指向previousViewController.navigationItem。在viewWillLayoutSubviews中,topItem和backItem被更新。在第一次viewWillAppear之后,我们应该设置backitem。backbarbuttonitem。
答:设置一个backBarButtonItem到前一个viewController的navigationItem,无论何时何地在你的当前viewController(顶部viewController)。你可以在viewWillAppear或viewDidLoad中使用这段代码。查看我的博客文章iOS设置导航栏返回按钮标题的详细分析。
NSArray *viewControllerArray = [self.navigationController viewControllers];
// get index of the previous ViewContoller
long previousIndex = [viewControllerArray indexOfObject:self] - 1;
if (previousIndex >= 0) {
UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:backButtonTitle
style:UIBarButtonItemStylePlain
target:self
action:nil];
}
使用下面的代码行:
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"hello"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItems =[[NSArray alloc] initWithObjects:newBackButton, nil];
self.navigationItem.leftItemsSupplementBackButton = YES;
我在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是显示的。
编辑:这也保持了后退按钮的原始外观(左边带箭头的栏按钮项)。