Create and Initialise Custom UITabBarItem Class

时间:2018-04-20 21:31:00

标签: ios swift uitabbar uitabbaritem

I am building an application with four UITabBarItems. They all should look the same. Instead of customising them in their respective UIViewController, I am trying to build a custom UITabBarItem class and have the UITabBarItems inherit from it.

So far, this is the work I have done:

class CustomTabBarItem: UITabBarItem {


    func customiseTabBarItems() {
        // Customises the item on the tab bar

        // For normal state
       setTitleTextAttributes([NSAttributedStringKey.font : Fonts.small!, NSAttributedStringKey.foregroundColor: Colours.grey], for: .normal)

        // When highlighted
       setTitleTextAttributes([NSAttributedStringKey.font : Fonts.small!, NSAttributedStringKey.foregroundColor: Colours.white], for: .highlighted)
    }


}

and I have set each UITabBarItem as a subclass of my 'CustomTabBarItem' in the storyboard.

The issue, however, is that the functionality is not showing up for my UITabBarItems.

Any advice?

1 个答案:

答案 0 :(得分:0)

  

我正在使用四个UITabBarItem构建一个应用程序。他们都应该看起来一样

实现这一目标的方法不是尝试某种UITabBarItem子类,而是将appearance proxy用于UITabBarItem。例如:

UITabBarItem.appearance().setTitleTextAttributes( // ...

在您的app app didFinishLaunching中执行此操作。所有标签栏项目都将根据您对外观代理的命令进行自定义。