在Swift for iOS中将TabBarItem标题字体更改为粗体

我试图将所选标签栏项目的字体粗细设置为粗体字体。 这似乎没有效果。 任何想法是什么错误。 forState: .Normal按预期工作, forState: .Selected无效。

 let tabBarItem0 = tabBar.items![0] as! UITabBarItem var selectedImage0 : UIImage = UIImage(named:"ic_tabbar_item_one")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) var fontLight:UIFont = UIFont(name: "HelveticaNeue-UltraLight", size: 12)! var fontBold:UIFont = UIFont(name: "HelveticaNeue-Bold", size: 12)! tabBarItem0.image = unselectedImage0 tabBarItem0.selectedImage = selectedImage0 tabBarItem0.title = "Overview" tabBarItem0.setTitleTextAttributes( [ NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: fontLight ], forState: .Normal) tabBarItem0.setTitleTextAttributes( [ NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: fontBold ], forState: UIControlState.Selected) 

find解决scheme (Swift 3,XCode 8.1)

  1. 在Storyboard中,为每个UITabBarItem提供一个唯一的标签:对于每个标签 – >select它并转到它的“属性检查器” – >在“标签”字段中给每一个唯一的编号,但不应该使用零使用1到4)。

    这为我们稍后设置,以确定哪个标签被按下。


  1. 创build一个UITabBarController的新子类,然后赋值它 :FILE – > New File – > iOS Cocoa Touch – >创buildUITabBarController的子类。 在“Identity Inspector”下将新的.swift文件分配给您的UITabBarController。

    我们需要在我们的UITabBarController中定制逻辑。


  1. 创build一个新的UITabBarItem的子类,将相同的文件分配给所有的UITabBarItems :FILE – > New File – > iOS Cocoa Touch – >创buildUITabBarItem的子类,并将同一个分配给所有的标签。

    我们需要在标签栏项目中使用共享的自定义逻辑。


  1. 将此代码添加到您的UITabBarItem子类中 ,它将设置初始状态(主标签粗体,其余未选定),并允许编程标签更改:

     class MyUITabBarItemSubclass: UITabBarItem { //choose initial state fonts and weights here let normalTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightRegular) let selectedTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightBold) //choose initial state colors here let normalTitleColor = UIColor.gray let selectedTitleColor = UIColor.black //assigns the proper initial state logic when each tab instantiates override func awakeFromNib() { super.awakeFromNib() //this tag # should be your primary tab's Tag* if self.tag == 1 { self.setTitleTextAttributes([NSFontAttributeName: selectedTitleFont, NSForegroundColorAttributeName: selectedTitleColor], for: UIControlState.normal) } else { self.setTitleTextAttributes([NSFontAttributeName: normalTitleFont, NSForegroundColorAttributeName: normalTitleColor], for: UIControlState.normal) } } } 

在这里,我们设置了初始状态,以便在应用程序打开时正确设置选项卡,我们将负责下一个子类中的物理选项卡按下。


  1. 将此代码添加到您的UITabBarController子类中 ,这是在按Tab键时分配正确状态的逻辑。

     class MyUITabBarControllerSubclass: UITabBarController { //choose normal and selected fonts here let normalTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightRegular) let selectedTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightBold) //choose normal and selected colors here let normalTitleColor = UIColor.gray let selectedTitleColor = UIColor.black //the following is a delegate method from the UITabBar protocol that's available //to UITabBarController automatically. It sends us information every //time a tab is pressed. Since we Tagged our tabs earlier, we'll know which one was pressed, //and pass that identifier into a function to set our button states for us override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { setButtonStates(itemTag: item.tag) } //the function takes the tabBar.tag as an Int func setButtonStates (itemTag: Int) { //making an array of all the tabs let tabs = self.tabBar.items //looping through and setting the states var x = 0 while x < (tabs?.count)! { if tabs?[x].tag == itemTag { tabs?[x].setTitleTextAttributes([NSFontAttributeName: selectedTitleFont, NSForegroundColorAttributeName: selectedTitleColor], for: UIControlState.normal) } else { tabs?[x].setTitleTextAttributes([NSFontAttributeName: normalTitleFont, NSForegroundColorAttributeName: normalTitleColor], for: UIControlState.normal) } x += 1 } } } 

看起来这是如此的痛苦,因为由于某种原因,标签不能识别状态变化为“.Selected”。 我们必须通过只处理正常状态来做所有事情 – 基本上检测到状态的变化。


  1. 您可以通过编程方式更改选项卡,并仍然可以检测到状态更改…如果任何人有兴趣,我会稍后更新,请询问。

希望这有助于!

 UITabBarItem.appearance().setTitleTextAttributes( [NSFontAttributeName: UIFont(name:"your_font_name", size:11)!, NSForegroundColorAttributeName: UIColor(rgb: 0x929292)], forState: .Normal) 

尝试更改所选项目的字体时遇到同样的问题。 看起来像titleTextAttributes的字体参数仅在设置为正常状态时才有用。 这就是为什么我实现UITabBarControllerDelegate在哪里我更新当前选定项目的属性。 你也应该在UITabBarControllerloadView()之后调用updateSelection()方法。 或者你可以在覆盖selectedItem设置器中调用updateSelection()方法。

 extension TabBarController: UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) { updateSelection() } func updateSelection() { let normalFont = Fonts.Lato.light.withSize(10) let selectedFont = Fonts.Lato.bold.withSize(10) viewControllers?.forEach { let selected = $0 == self.selectedViewController $0.tabBarItem.setTitleTextAttributes([.font: selected ? selectedFont : normalFont], for: .normal) } } } 

要设置TitleTextAttribute ,您应该使用appearance代理,如: [UIBarItem appearance]

问题是tabBarItem0的状态不会更改为Selected 。 因为这是代表UITabBarItem单个元素的UITabBar 。 所以,你不能直接改变使用UITabBarItem API的状态。 你必须通过分配selectedItem来改变它的状态。

这些信息是从文档中获得的,我build议所有程序员都有这样的技能。 希望这会有所帮助。