0

I have a tabBar controller and the items have no text, I followed this answer to do it

I want to add text to only the middle tabBarItem, how should I add it?

class MainTabBarController: UITabBarController, UITabBarControllerDelegate {


override func viewDidLoad() {
    super.viewDidLoad()

    delegate = self

    configureTabs()
}

override func viewDidLayoutSubviews() {

    var verticalOffset: CGFloat = 6.0

    if #available(iOS 11.0, *), traitCollection.horizontalSizeClass == .regular {
        verticalOffset = 0.0
    }

    let imageInset = UIEdgeInsets(
        top: verticalOffset,
        left: 0.0,
        bottom: -verticalOffset,
        right: 0.0
    )

    for tabBarItem in tabBar.items ?? [] {
        tabBarItem.title = ""
        tabBarItem.imageInsets = imageInset
    }
}

fileprivate func configureTabs() {

    let homeContainerVC = HomeContainerController()
    let navVCZero = UINavigationController(rootViewController: homeContainerVC)
    navVCZero.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "homeIcon"), tag: 0)

    let discoverVC = PopularDiscoverContainerController()
    let navVCOne = UINavigationController(rootViewController: discoverVC)
    navVCOne.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "discoverIcon"), tag: 1)

    let cameraVC = CameraController()
    cameraVC.tabBarItem = UITabBarItem(title: "Post Something", image: UIImage(named: "cameraIcon"), tag: 2)

    let notifcationsVC = NotifcationsController()
    let navVCThree = UINavigationController(rootViewController: notifcationsVC)
    navVCThree.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "notificationsIcon"), tag: 3)

    let settingsVC = SettingsController()
    let navVCFour = UINavigationController(rootViewController: settingsVC)
    navVCFour.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "settingsIcon"), tag: 4)

    viewControllers = [navVCZero, navVCOne, cameraVC, navVCThree, navVCFour]
}

//MARK:- TabBarController Delegate
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

    // this makes sure it's the vc you want to present modally from the tab index
    if viewController is CameraController {

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.clear
        let cameraVC = CameraController()
        let navVC = UINavigationController(rootViewController: cameraVC)
        navVC.modalPresentationStyle = .fullScreen
        navVC.modalPresentationCapturesStatusBarAppearance = true
        tabBarController.present(navVC, animated: true, completion: nil)

        // return false to have the tab present it
        return false
    }

    // return true to go back to the tab you was initially on before you pressed it
    return true
}
}

App Delegate:

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        UITabBar.appearance().tintColor = UIColor.black
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 11)], for: .normal)
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

0

I got the answer. In viewDidLayoutSubviews I only had to add:

for tabBarItem in tabBar.items ?? [] {
    if tabBarItem.tag != 2 {
        tabBarItem.title = ""
        tabBarItem.imageInsets = imageInset
    }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256