ios 在 Swift 中以编程方式设置 tabBarItem 标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27407590/
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
Programmatically setting tabBarItem title in Swift
提问by Sam
I have four UIViewControllers that are linked to a UITabBarController's tab bar. I need to set the tab bar item titles outside of the storyboard, and inside of their classes.
我有四个 UIViewControllers 链接到一个 UITabBarController 的标签栏。我需要在故事板之外和它们的类内设置标签栏项目标题。
I've tried..
我试过了..
class MyViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
}
}
This is called, but the title is never set. Same with self.tabBarItem.title = "the title"
这被调用,但从未设置标题。与 self.tabBarItem.title = "the title" 相同
I've also tried setting the title in viewDidLoad, but that only updates the title after going to the view controller.
我也试过在 viewDidLoad 中设置标题,但这只会在进入视图控制器后更新标题。
Thoughts?
想法?
采纳答案by Sam
I figured it out, looks like it was being over written by awakeFromNib().
我想通了,看起来它是由awakeFromNib() 覆盖的。
override func awakeFromNib() {
super.awakeFromNib()
self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
}
I moved my self.title assignment there and it corrected my issue.
我将我的 self.title 分配移到那里,它纠正了我的问题。
回答by keithbhunter
You can set the tab titles in the view controllers themselves in viewDidLoad
by setting the view controller's title
property.
您可以viewDidLoad
通过设置视图控制器的title
属性在视图控制器本身中设置选项卡标题。
title = "Number 0"
Alternatively, if want to set the titles from your tab bar controller, you can set them like this in your tab bar controller's viewDidLoad
:
或者,如果要从选项卡栏控制器设置标题,您可以在选项卡栏控制器中像这样设置它们viewDidLoad
:
tabBar.items?[0].title = "Number 0"
tabBar.items?[1].title = "Number 1"
回答by Ginés SM
I've been trying different solutions but the only one what worked for me was adding the tab bar set up the code in the viewWillAppear
method in the UITabBarController
. I don't do it in each view controller individually because it works only when the tab bar button is pressed:
我一直在尝试不同的解决方案,但唯一对我有用的viewWillAppear
方法是在UITabBarController
. 我不会在每个视图控制器中单独执行此操作,因为它仅在按下选项卡栏按钮时才起作用:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let items = tabBar.items else { return }
items[0].title = "Title0"
items[1].title = "Title1"
items[2].title = "Title2"
items[3].title = "Title3"
}
回答by Poran Ramen
Here is the solution in every viewController.swift file you can just add the following code
这是每个 viewController.swift 文件中的解决方案,您只需添加以下代码即可
override func awakeFromNib() {
self.tabBarItem.title = "title"
self.tabBarItem.image = "image.png"
}
That's a super easy awakeFromNib method and you can easily call any thing when nib has created simple on start because in ViewDidLoad or any viewControllerDelegate method called when you clicked or select. So that's a super easy function.
这是一个超级简单的awakeFromNib 方法,当nib 在开始时创建简单时,您可以轻松调用任何东西,因为在ViewDidLoad 或单击或选择时调用的任何viewControllerDelegate 方法中。所以这是一个超级简单的功能。
Thanks.
谢谢。
回答by Denis Kutlubaev
If you create your ViewControllers
programmatically before putting to the TabBarController
, try this:
如果您ViewControllers
在放入 之前以编程方式创建TabBarController
,请尝试以下操作:
override init(style: UITableViewStyle) {
super.init(style: style)
self.title = "Title"
}
or this, if it is not a TableViewController
:
或者这个,如果它不是TableViewController
:
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.title = "Title"
}