简体   繁体   English

快速更改情节提要标签栏图标

[英]change storyboard tab bar icon using swift

I've created a tab bar controller and embedded several view controllers in it. 我创建了一个标签栏控制器,并在其中嵌入了几个视图控制器。 I then try to set a specific tab bar item in a corresponding swift file for that view. 然后,我尝试在该视图的相应swift文件中设置特定的选项卡栏项目。 Problem is that I am changing the item by overriding the "viewDidLoad" function. 问题是我要通过覆盖“ viewDidLoad”函数来更改项目。 So it only updates after the user has touched that item. 因此,它仅在用户触摸该项目后更新。 What be a better approach to changing storyboard tab bar items using swift? 使用swift更改情节提要选项卡栏项目的更好方法是什么?

  1. create a subclass of UITabBarController 创建UITabBarController的子类
  2. set this custom class of your TabBarController 设置您的TabBarController的自定义类
  3. now you override UITabBarController ViewDidLoad Method 现在您重写UITabBarController ViewDidLoad方法
  4. there you can access all the TabItems and change their text/images before a ViewController get loaded. 在那里,您可以访问所有TabItem并在加载ViewController之前更改其文本/图像。

      class CustomTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() let allItems:[AnyObject] = self.tabBar.items! var item1:UITabBarItem = allItems[0] as! UITabBarItem var item2:UITabBarItem = allItems[1] as! UITabBarItem item1.image = UIImage(named: "menu@2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) item2.image = UIImage(named: "play@2x.png")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal) } 

    } }

in custom class the code should be like as above... you can set selectedState Image as well.... 在自定义类中,代码应与上面类似。您也可以设置selectedState Image。

here is the result.. 这是结果。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM