【问题标题】:How to make Custom TabBar?如何制作自定义标签栏?
【发布时间】:2021-07-08 10:23:59
【问题描述】:

我必须像上图那样自定义标签栏。但是,我不知道如何自定义这一点,因为只有在选择标签栏时才会出现项目名称,如果不选择则不应该出现项目名称。请帮帮我。

【问题讨论】:

标签: ios swift storyboard tabbar


【解决方案1】:

例如,它是用两个项目编写的。
您可以根据didSelect() 方法中的选定项分支到tag
viewWillAppear() 中,我写了第一项的标题,因为在首次启动应用程序时选择了第一项。 (初始化)

希望我的回答对你有帮助。

TabBarController.swift

import UIKit

class TabBarController: UITabBarController, UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
    
        //Setting the UITabBarItem
        
        let tab1 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController")
        let tab1BarItem = UITabBarItem(title: "home", image: UIImage(systemName: "seal"), selectedImage: UIImage(systemName: "seal.fill"))
        tab1.tabBarItem = tab1BarItem
        tab1.tabBarItem.tag = 0
        
        let tab2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "SearchViewController")
        let tab2BarItem = UITabBarItem(title: "", image: UIImage(systemName: "checkmark.seal"), selectedImage: UIImage(systemName: "checkmark.seal.fill"))
        tab2.tabBarItem = tab2BarItem
        tab2.tabBarItem.tag = 1
        
        self.viewControllers = [tab1, tab2]
    }
    

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        if item.tag == 0 { // tab1(home)
            item.title = "home"
            tabBar.items?[1].title = ""
        }
        
        if item.tag == 1 { // tab2(search)
            item.title = "search"
            tabBar.items?[0].title = ""
        }
    }
    
}

预览

【讨论】:

    【解决方案2】:

    你需要做两件事

    1- 使选定的标签颜色变为绿色

    tabController.tabBar.tintColor = UIColor.green
    

    2- 当项目被选中时,监听 UITabBarControllerDelegate 并为每个未选中的项目分配 text 设置为 "" 的项目

    tabController.tabBar.items = [] // set all items  
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2012-05-24
    • 1970-01-01
    • 2012-05-07
    • 2015-10-16
    相关资源
    最近更新 更多