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

Tho*_*mel 5 uikit ios swift

我正在尝试将所选标签栏项目的字体粗细设置为粗体。似乎没有效果。知道什么地方错了。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)
Run Code Online (Sandbox Code Playgroud)

Abh*_*bhi 6

找到解决方案(Swift 3、XCode 8.1)

  1. 在 Storyboard 中,为您拥有的每个 UITabBarItem 指定一个唯一的标签:对于每个选项卡 -> 选择它并转到它的“属性检查器” -> 在“标签”字段中为每个选项卡指定一个唯一的编号,但不应使用零(我使用 1 至 4)。

    这为我们以后识别按下了哪个选项卡做好了准备。


  1. 创建 UITabBarController 的新子类,然后分配它:FILE -> New File -> iOS Cocoa Touch -> 创建 UITabBarController 的子类。将新的 .swift 文件分配给“Identity Inspector”下的 UITabBarController。

    我们需要在 UITabBarController 中自定义逻辑。


  1. 创建一个新的 UITabBarItem 子类,将相同的文件分配给所有 UITabBarItems:文件 -> 新文件 -> iOS Cocoa Touch -> 创建 UITabBarItem 的子类并将相同的文件分配给所有选项卡。

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


  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)
            }
    
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

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


  1. 将此代码添加到您的 UITabBarController 子类中,它是在您按下选项卡时分配正确状态的逻辑。

    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
    
            }
    
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

看起来这真是太痛苦了,因为由于某种原因,选项卡无法识别状态更改为“.Selected”。我们必须通过仅使用正常状态来完成所有工作 - 基本上是我们自己检测状态变化。


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

希望这有帮助!


Luc*_*ang 1

问题是 的 状态tabBarItem0没有更改为Selected。因为这UITabBarItem代表了 a 的单个元素UITabBar。因此,您不能直接使用UITabBarItemAPI 更改状态。您必须通过分配来更改它的状态selectedItem

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