选择时如何显示TabBar的标题?

时间:2019-12-20 09:22:22

标签: swift uitabbar tabbar

我的TabBar有5个选项卡,我以编程方式使它具有标题,但是我想在运行应用程序时隐藏标题,当我单击每个选项卡时该选项卡都显示标题

导入UIKit

MainVC类:UITabBarController,UITabBarControllerDelegate {

override func viewDidLoad() {
    super.viewDidLoad()
    self.delegate = self
    self.view.backgroundColor = .yellow
    tabBar.tintColor = UIColor.init(displayP3Red: 0, green: 0, blue: 220, alpha: 1)

     setuptabbar()
}
func setuptabbar() {

    let hometab = UINavigationController(rootViewController: homeVC())
    hometab.tabBarItem.image = UIImage(named: "home-off")
    hometab.tabBarItem.selectedImage = UIImage(named: "home-on")
    hometab.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0);
    hometab.title = "save"

    let RightTab = UINavigationController(rootViewController: page1())
    RightTab.tabBarItem.image = UIImage(named: "his-off1")
    RightTab.tabBarItem.selectedImage = UIImage(named: "his-on1")
    RightTab.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: 0);

    RightTab.title =  "home"

    let LeftTab = UINavigationController(rootViewController: page2())
    LeftTab.tabBarItem.image = UIImage(named: "support")
    LeftTab.tabBarItem.selectedImage = UIImage(named: "addmember")
    LeftTab.title = "search"


    let RightTab2 = UINavigationController(rootViewController: page4())
    RightTab2.tabBarItem.image = UIImage(named: "share")
    RightTab2.tabBarItem.selectedImage = UIImage(named: "support")
    RightTab2.title = "profile"


    let LeftTab2 = UINavigationController(rootViewController: page5())
    LeftTab2.tabBarItem.image = UIImage(named: "title_icon")
    LeftTab2.tabBarItem.selectedImage = UIImage(named: "addmember")
    viewControllers = [RightTab  , LeftTab, hometab , RightTab2 , LeftTab2 ]
    guard let items = tabBar.items else {return}
    for item in items{
        item.imageInsets = UIEdgeInsets(top: 0, left: 0, bottom:0, right: 0)


    }



}

}

1 个答案:

答案 0 :(得分:0)

  • 设置默认的屏幕标题取消隐藏,其他所有baritem的标题隐藏(使用viewdidload)
  • 单击选项卡按钮时取消隐藏该项目的标题并隐藏所有 其他标题

如果您不想隐藏标题,也可以通过设置相同的颜色来实现。

这是将要使用的一些代码

self.navigationItem.title = @"my title"; 

设置导航栏标题。

self.tabBarItem.title = @"my title";

设置标签栏标题。

self.title = @"my title";

设置这两个。 我希望它对您有用...:)