[英]Programmatically set the UITabBarItem icon for every linked ViewController
Currently we are changing the image shown in the UITabBarItem like this:目前我们正在像这样更改 UITabBarItem 中显示的图像:
override func viewDidLoad() {
super.viewDidLoad()
// register additional NIB files
tableView.registerNib(UINib(nibName: "WeekDayTableViewCell", bundle: nil), forCellReuseIdentifier: "WeekDayCell")
tabBarItem.title = "Week".localized
tabBarItem.image = UIImage.fontAwesomeIconWithName(FontAwesome.Calendar, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
}
The problem with this is, that you have to click on every tab to load the corresponding images.这样做的问题是,您必须单击每个选项卡才能加载相应的图像。 I thought I could change the images in the UITabBarViewController if I get the list of all UITabBarItems.
如果我得到所有 UITabBarItems 的列表,我想我可以更改 UITabBarViewController 中的图像。 But this list is always empty.
但是这个列表总是空的。
Which is the correct way to do this?这样做的正确方法是什么?
An alternative—perhaps a more Swift-y—way to do this is to set the icons from a Dictionary of tabs.另一种方法——也许是更 Swift-y 的方法是从选项卡字典中设置图标。 We have an enum
TabTitles
for the names of the tabs, and tabIcons
to look it up.我们有一个用于选项卡名称的枚举
TabTitles
和用于查找它的tabIcons
。
This way, there's not so much to remember to change if you add a tab or change the order.这样,如果您添加选项卡或更改顺序,则无需记住要更改的太多内容。
private enum TabTitles: String, CustomStringConvertible {
case Stacks
case Settings
case Clusters
case Services
case Registries
private var description: String {
return self.rawValue
}
}
private var tabIcons = [
TabTitles.Stacks: FontAwesome.Clone,
TabTitles.Settings: FontAwesome.Gears,
TabTitles.Clusters: FontAwesome.Cubes,
TabTitles.Services: FontAwesome.Exchange,
TabTitles.Registries: FontAwesome.Institution
]
override func viewDidLoad() {
super.viewDidLoad()
if let tabBarItems = tabBar.items {
for item in tabBarItems {
if let title = item.title,
tab = TabTitles(rawValue: title),
glyph = tabIcons[tab] {
item.image = UIImage.fontAwesomeIconWithName(glyph, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
}
}
}
}
I found out, if I add this code to the viewDidAppear
in the UITabBarController it will work.我发现,如果我将此代码添加到 UITabBarController 中的
viewDidAppear
它将起作用。
let tabBarItems = tabBar.items! as [UITabBarItem]
tabBarItems[0].title = "Week".localized
tabBarItems[0].image = UIImage.fontAwesomeIconWithName(FontAwesome.Calendar, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
tabBarItems[1].title = "Settings".localized
tabBarItems[1].image = UIImage.fontAwesomeIconWithName(FontAwesome.Gears, textColor: UIColor.blueColor(), size: CGSizeMake(30, 30))
Use item.image = UIImage.fontAwesomeIcon(name: glyph, textColor: UIColor.black, size: CGSize(width: 30, height: 30))
for Swift 3. item.image = UIImage.fontAwesomeIcon(name: glyph, textColor: UIColor.black, size: CGSize(width: 30, height: 30))
使用item.image = UIImage.fontAwesomeIcon(name: glyph, textColor: UIColor.black, size: CGSize(width: 30, height: 30))
。
Also remember to import FontAwesome_swift还记得导入 FontAwesome_swift
Swift 5斯威夫特 5
You can set tabBarItems icons from your first controller 'viewDidLoad' method.您可以从您的第一个控制器“viewDidLoad”方法设置 tabBarItems 图标。
func setupTabBar() {
tabBarController?.tabBar.items![0].title = "Week"
tabBarController?.tabBar.items![0].image = #imageLiteral(resourceName: "icons8-clinic")
tabBarController?.tabBar.items![1].title = "Profile"
tabBarController?.tabBar.items![1].image = #imageLiteral(resourceName: "icons8-phone_not_being_used_filled")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.