如何在特定选项卡项下而不是其他项下添加标题

时间:2018-10-07 20:07:19

标签: ios swift uitabbarcontroller

我有一个tabBar控制器,并且项目没有文本,我遵循了this answer to do it

我只想向中间的tabBarItem添加文本,应该如何添加文本?​​

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
}
}

应用代理:

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)
    }
}

1 个答案:

答案 0 :(得分:0)

我得到了答案。在viewDidLayoutSubviews中,我只需要添加:

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