I'm attempting to programmatically add unique viewControllers to TabBarItems and create a UITabBar controller using an IBOutlet.
Here is my code for creating UITabBar
public class TabBarViewController: UIViewController, UITabBarDelegate {
@IBOutlet weak var tabBarCnt: UITabBar!
override public func viewDidLoad() {
super.viewDidLoad()
self.tabBarCnt.delegate = self
createTabBarController()
}
func createTabBarController() {
let firstVC = FirstViewController()
firstVC.name = nameVal
firstVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil), tag: 0)
firstVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);
let secondVC = SecondViewController()
firstVC.name = nameVal
firstVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil), tag: 0)
firstVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);
let thirdVC = ThirdViewController()
thirdVC.name = nameVal
thirdVC.tabBarItem = UITabBarItem.init(title: nil, image: UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil), tag: 0)
thirdVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);
tabBarCnt.items = [firstVC, secondVC, thirdVC]
self.view.bringSubviewToFront(tabBarCnt)
}
}
It does not display any viewControllers.
How to create a UITabBar based on my code?
A UITabBar does not display any view controllers. It only displays tabs.
Most likely you should be using a UITabBarController, not a standalone UITabBar. Change your TabBarViewController class to extend UITabBarController instead of UIViewController and eliminate the UITabBar outlet.
public class TabBarViewController: UITabBarController {
override public func viewDidLoad() {
super.viewDidLoad()
setupViewControllers()
}
func setupViewControllers() {
let firstVC = FirstViewController()
firstVC.name = nameVal
firstVC.tabBarItem.image = UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil)
firstVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);
let secondVC = SecondViewController()
secondVC.name = nameVal
secondVC.tabBarItem.image = UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil)
secondVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);
let thirdVC = ThirdViewController()
thirdVC.name = nameVal
thirdVC.tabBarItem.image = UIImage(named: "ico_active", in: Bundle(for: TabBarViewController.self), compatibleWith: nil)
thirdVC.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0);
viewControllers = [firstVC, secondVC, thirdVC]
}
}
Note that you do not need to create new instances of UITabBarItem for each view controller.
You can also add an extension to your view controller to handle the tab bar controller delegate methods. Something like the following:
extension TabBarViewController: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// Handle the user selecting a different tab
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With