简体   繁体   English

删除导航栏中的后退按钮文本和中心标题

[英]Remove back button text and center title in navigation bar

I've removed the text for UIBarButton in AppDelegate: 我在AppDelegate中删除了UIBarButton的文本:

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -1000.f) forBarMetrics:UIBarMetricsDefault];

Which works like a charm: 这就像一个魅力:

UINavigationBar的

As you can see, this doesn't align the navigation title at horizontal center. 如您所见,这不会将导航标题与水平中心对齐。 What is the best solution to accomplish this globally for all views. 对于所有视图,全局实现此目标的最佳解决方案是什么。

PS: I am using Storyboard. PS:我正在使用Storyboard。

You can create your own custom titleView with a UILabel as follows: 您可以使用UILabel创建自己的自定义titleView,如下所示:

UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0,0,100,50)];
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,100,50)];

titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"PAKKELISTE";
[titleView addSubview:titleLabel];

self.navigationItem.titleView = titleView;

The details of the frames, text, alignment, etc. are just an example. 框架,文本,对齐等的细节只是一个例子。 The main idea though is that you set a custom UIView as the navigationItem's titleView. 但主要的想法是将自定义UIView设置为navigationItem的titleView。

It could also be an issue with your back button offset. 你的后退按钮偏移也可能是一个问题。 Try this approach instead for removing the "back" text (I haven't tried this before, but I'm curious if it will work). 尝试这种方法,而不是删除“后面”文本(我之前没有尝试过,但我很好奇它是否会起作用)。

self.navigationController.navigationBar.topItem.title = @"";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM