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

Posted

技术标签:

【中文标题】如何在特定标签栏项而不是其他项下添加标题【英文标题】:How to add a Title under a Specific Tabbar Item and not others 【发布时间】:2018-10-07 20:07:19 【问题描述】:

我有一个 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】:

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

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

【讨论】:

以上是关于如何在特定标签栏项而不是其他项下添加标题的主要内容,如果未能解决你的问题,请参考以下文章

如何允许标签栏项具有多个视图控制器

添加选项卡栏项 (UITabBar) 时 XCode 4.3 崩溃

如何从 UIButton 到选项卡栏项?

iOS 标签栏项自定义图像

增加标签栏项目之一的高度

有条件地在选项卡栏项后面选择viewcontroller