Tab bar not showing icons?

你说的曾经没有我的故事 提交于 2019-12-03 23:16:58

Problem With Storyboard References and Missing Tab Bar Icons

If you have Storyboard References attached to your Tab Bar Controller you will notice that the storyboard reference actually has a UITabBarItem on it:

Do not set the properties for this "fake" UITabBarItem!

Your settings will do nothing at all when you run the app. The values you provide ONLY affect what you see in Interface Builder, not your app when you run it.

Solution

What you need to do is go to the ACTUAL view controller that your storyboard reference is pointing to. That view controller will also have a UITabBarItem on it. This is the "real" UITabBarItem. Set your properties (icon, title, etc.) there and you will see them show when you run the app.

I have attached image describing connections:

  1. Connect TabBar Controller to your five View Controllers

  2. For each View Controller, select any View Controller -> Go to Editor in Xcode options at top -> Embed In -> Navigation Controller

  3. Select bottom bar in Navigation Controller -> open Attribute Inspector in Utilities -> provide the your title and image as highlighted in image

Navigation from Login/Register to TabBar Screen: Provide the Storyboard ID to TabBarController eg. "TabBarController" and create the TabBarController instance using Storyboard ID of corresponding Storyboard.

Swift 3

func navigateToTabBar() {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TabBarController") as UIViewController
    self.present(nextViewController, animated:true, completion:nil)
}

If you are making your navigationController programmatically, you will see the same symptoms due to viewDidLoad being called too late. Overriding init in your child view controllers will get your icons showing properly.

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    self.tabBarItem = UITabBarItem(title: "Settings", image: UIImage(named: "gear"), tag: 1)
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
}

Happy coding!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!