ios 故事板中的 UIViewController Title 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18844997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIViewController Title attribute in Storyboard
提问by 0xSina
I am setting the title field of a UIViewController
via Interface Builder/Storyboard:
我正在UIViewController
通过Interface Builder/Storyboard设置标题字段:
This view controller is nested in a UINavigationController
which in turn is nested within a UITabBarController
. When I run the app, I my navigation item has no title, neither does the tab bar item.
此视图控制器嵌套在 aUINavigationController
中,而a又嵌套在UITabBarController
. 当我运行应用程序时,我的导航项没有标题,标签栏项也没有。
If I explicitly set the view controller's navigation item's title, and also it's tab bar item's title in interface builder, then it works just fine.
如果我明确设置了视图控制器的导航项的标题,并且它也是界面构建器中选项卡栏项的标题,那么它就可以正常工作。
I am wondering:
我想知道:
a)If I am not using Storyboardbut just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behaviour?
a) 如果我不使用Storyboard而只是使用常规的xibs,则设置视图控制器的标题会隐式设置导航项的标题以及选项卡栏项的标题。但它不是同一个故事板。这是预期的行为吗?
b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.
b) 那么视图控制器标题(在Storyboard 中)的目的是什么?它似乎没有效果。
Thanks!
谢谢!
采纳答案by hgwhittle
You can set the title of the UINavigationBar
in Storyboard by double clicking the actual navigationBar and typing in a title right there. This only sets the title for the UINavigationBar
.
您可以UINavigationBar
通过双击实际的导航栏并在那里输入标题来设置故事板中的标题。这只会设置UINavigationBar
.
Setting the title in code offers some different possibilities.
在代码中设置标题提供了一些不同的可能性。
self.title = @"Your title";
will set the title of a navigationBar and also cause the title to cascade down to a UITabBarItem, if present.
self.title = @"Your title";
将设置导航栏的标题,并导致标题向下级联到 UITabBarItem(如果存在)。
self.navigationItem.title = @"Your title";
will only set the title of the navigationBar, assuming a UINavigationController is present, and NOT affect a UITabBarItem.
self.navigationItem.title = @"Your title";
只会设置导航栏的标题,假设存在 UINavigationController,并且不会影响 UITabBarItem。
self.navigationController.title = @"Your title";
will set the title of a UITabBarItem but NOT the UINavigationBar.
self.navigationController.title = @"Your title";
将设置 UITabBarItem 的标题而不是 UINavigationBar。
回答by Mario
Step 1
第1步
If you're looking at a Xib in Xcode's Interface Builder, take a look in the "Document Outline" panel (second panel from the left). Expand the view controller you're working with until you find an icon labelled: Navigation Item.
如果您正在 Xcode 的 Interface Builder 中查看 Xib,请查看“文档大纲”面板(左侧第二个面板)。展开您正在使用的视图控制器,直到找到一个标记为:导航项的图标。
Step 2
第2步
If you then highlight the Navigation Item and open up the Utilities panel (the farthest on the right), and click the Attributes Inspector, you'll see where you can set the title of the view controller. This is the way to do it in Interface Builder, rather than doing it through code.
如果然后突出显示 Navigation Item 并打开 Utilities 面板(最右侧的),然后单击 Attributes Inspector,您将看到可以设置视图控制器标题的位置。这是在 Interface Builder 中完成的方法,而不是通过代码来完成。
回答by Cora Middleton
I ran into this issue this morning. Here are the stabs I took and the final workaround.
我今天早上遇到了这个问题。这是我的尝试和最终的解决方法。
This correctly logs the child view controller's title as set in the storyboard, but has no effect on what's being presented:
这正确地记录了故事板中设置的子视图控制器的标题,但对呈现的内容没有影响:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Title: %@", self.title);
}
This has no effect; the title still doesn't show (probably doing an "if (![_title isEqualToString:title]){}" user the hood:
这没有效果;标题仍然没有显示(可能是在执行“if (![_title isEqualToString:title]){}”用户引擎盖:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = [self.title copy];
}
This causes the title to be set correctly:
这会导致正确设置标题:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *title = self.title;
self.title = nil;
self.title = title;
}
My guess is that the UINavigationController is pulling the title for the view being presented before it has been loaded from the storyboard, but then doesn't start listening for changes until after the property has been set. I don't use storyboards or nibs very often, however, so it's quite possible there's a magic checkbox for this hidden somewhere that I've missed.
我的猜测是 UINavigationController 在从故事板加载之前拉取显示的视图的标题,但在设置属性之后才开始监听更改。然而,我不经常使用故事板或笔尖,所以很可能有一个我错过的隐藏位置的魔法复选框。
In any case, it looks like you can either do the self.navigationItem.title = self.title dance, or the above, as a workaround and still maintain your titles in IB.
在任何情况下,看起来你都可以做 self.navigationItem.title = self.title 舞蹈,或者上面的,作为一种解决方法,仍然保持你在 IB 中的头衔。
Apples docs for this are kindaclear:
这个苹果文档是还挺清楚的:
The navigation controller updates the middle of the navigation bar as follows:
If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller's navigation item.
If no custom title view is set, the navigation bar displays a label containing the view controller's default title. The string for this label is usuallyobtained from the title property of the view controller itself. If you want to display a different title than the one associated with the view controller, set the title property of the view controller's navigation item instead.
导航控制器更新导航栏中间如下:
如果新的顶级视图控制器具有自定义标题视图,则导航栏会显示该视图来代替默认的标题视图。要指定自定义标题视图,请设置视图控制器导航项的 titleView 属性。
如果没有设置自定义标题视图,导航栏会显示一个包含视图控制器默认标题的标签。这个标签的字符串通常是从视图控制器本身的 title 属性中获得的。如果要显示与视图控制器关联的标题不同的标题,请改为设置视图控制器导航项的 title 属性。
Emphasis mine.
强调我的。
回答by Julian F. Weinert
I just ran into the same problem. I don't understand why it's not working... It might be on purpose or just be a bug.
我刚刚遇到了同样的问题。我不明白为什么它不起作用......它可能是故意的,或者只是一个错误。
To change the title in interface builder, you can click on the navigation item directly and change the title there:
要在界面构建器中更改标题,您可以直接单击导航项并在那里更改标题:
回答by Jonny
Everything else on this page failed. For now, this worked, in code, in viewDidLoad:
此页面上的其他所有内容都失败了。目前,这在代码中有效,在 viewDidLoad 中:
NSString* text = @"My page title";
UIFont* font = [UIFont systemFontOfSize:20.0];
const CGSize SIZE = [text sizeWithAttributes:@{NSFontAttributeName:font}];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SIZE.width, SIZE.height)];
label.text = text;
label.textColor = UIColor.blackColor;
self.navigationItem.titleView = label;
回答by mbritto
If you have UINavigationItem
present, then you must use the navigation item title in the storyboard. If you don't have a navigation item for a view controller, then the UINavigationController
will use the view controller's title
property.
如果您有UINavigationItem
演示文稿,那么您必须使用故事板中的导航项标题。如果您没有视图控制器的导航项,UINavigationController
则将使用视图控制器的title
属性。
In your storyboard, if you have a navigation item the navigation controller doesn't need to read the view controller's title. Even if the title field is empty for this navigation item.
在您的故事板中,如果您有导航项,则导航控制器不需要读取视图控制器的标题。即使此导航项的标题字段为空。
Remove the navigation item (if you can, you won't be able to do it for the root view controller but you will for the others) and your title will be correctly loaded
删除导航项(如果可以,您将无法为根视图控制器执行此操作,但可以为其他视图控制器执行此操作)并且您的标题将被正确加载
回答by Bryan P
I tried all of the above methods, even tried manually adding a navigation bar but to no avail.
我尝试了上述所有方法,甚至尝试手动添加导航栏但无济于事。
So this is what worked for me.
所以这对我有用。
Remove any navigation bar item you manually added to the view controller and add this to your viewDidLoad
method
删除您手动添加到视图控制器的任何导航栏项目并将其添加到您的viewDidLoad
方法中
self.navigationController.navigationBar.topItem.title = @"My Title";
or
或者
self.navigationController.topViewController.title = @"My Title";
回答by Julian Corrêa
In my case I solve with this:
在我的情况下,我解决了这个:
[self.tabBarController.navigationItem setTitle:@"My Title"];
回答by Raphael Oliveira
I think it works as designed although we expect another behaviour. If you print the title property in - (void)viewDidLoadit will be the same value that you set in story board so I see no reason of this not working unless Apple's choice.
我认为它按设计工作,尽管我们期待另一种行为。如果您在- (void)viewDidLoad 中打印 title 属性,它将与您在故事板中设置的值相同,因此除非 Apple 选择,否则我认为没有理由不工作。
回答by Christian Di Lorenzo
a) If I am not using Storyboard but just regular xibs, setting the title of a view controller implicitly sets the navigation items' title as well as the tab bar item's title. But it's not the same storyboard. Is this the intended behavior?
a) 如果我不使用 Storyboard 而只是使用常规的 xibs,则设置视图控制器的标题会隐式设置导航项的标题以及选项卡栏项的标题。但这不是同一个故事板。这是预期的行为吗?
I believe this is the intended behavior. I think that the purpose of the title
attribute of a view controller is more of a property that can be used at the developer's discretion perhaps for distinguishing between controllers.
我相信这是预期的行为。我认为title
视图控制器属性的目的更多是一种属性,可以由开发人员自行决定使用,可能用于区分控制器。
Another reason for this change I think is that your navigation item's title may need to be different than the tab bar title, since the tab bar title cannot be nearly as long as the navigation title.
我认为此更改的另一个原因是您的导航项的标题可能需要与标签栏标题不同,因为标签栏标题不能与导航标题一样长。
b) What is then the purpose of the view controller's title (in Storyboard)? it seems to have no effect.
b) 那么视图控制器标题(在故事板中)的目的是什么?它似乎没有效果。
I think I mentioned this in my first paragraph. I think that the title
attribute of a controller is a property that the developer can use perhaps for distinguishing between controllers.
我想我在第一段中提到了这一点。我认为title
控制器的属性是开发人员可以用来区分控制器的属性。