How to hide the title on the tab bar item with navigation controller

时间:2016-04-12 00:26:50

标签: swift uinavigationcontroller uitabbarcontroller uitabbaritem

I have a tab bar item on navigation controller connected with tab bar controller and i want to delete the title in swift

Tab Bar Controller > Navigation Controller > View Controller

Tab Bar Item

Flow of the program

The application start with the tab bar controller with five tabs each one of these tabs are working fine i mean as hiding the title under the tab bar item but the tab in the image only have the problem of not been hidden and for that the application is also working on this tab okay if the user is logged out and the Viewcontroller in the image is showing but if the user is sign in the title on the tab bar item is showing so if there is away that i can hide the title programmatically

6 个答案:

答案 0 :(得分:10)

正如其他人所建议的那样,您可以转到Interface Builder并删除条形项目上的标题,也可以以编程方式执行。

只要您没有为选项卡链接到的UIViewController设置title属性,这就足够了。如果您要为视图控制器设置标题并避免其显示为条形项标题,请使用navigationItem.title = "My Title"代替title = "My Title"

答案 1 :(得分:4)

On Xcode go to your storyboard, after that, click on the navigation controller where the icon is set. Click on the tabBarItem at the bottom of the navigationController. On the left side go to the attribute inspector and erase the barItem title.

You can also do this programmatically, even though your storyboard will remain different.

let items = self.tabBarController?.tabBar.items
let tabItem = items![2]
tabItem.title = ""

答案 2 :(得分:2)

可以在UITabBarItem上为标签配置标题,该标题会被标签中的根视图控制器的标题覆盖。所以我想你在BookingViewController中设置了title = "Booking"

我没有明确隐藏标签标题的API,但你可以通过基本上将它们移出屏幕来隐藏它们:

let tabBarTitleOffset = UIOffsetMake(0,50)
for controller in tabBarController.viewControllers? {
    controller.tabBarItem.titlePositionAdjustment = tabBarTitleOffset
}

然后您的图标可能会显得略高,您也可以通过设置tabBarItem.imageInsets进行调整以补偿。

答案 3 :(得分:1)

我创建了UITabBarItem的子类,限制设置title,如下所示:

final class MyTabBarItem: UITabBarItem {

    override var title: String? {
        get { return nil }
        set { super.title = title }
    }

    override var imageInsets: UIEdgeInsets {
        get { return UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0) }
        set { super.imageInsets = imageInsets }
    }

    convenience init(image: UIImage? , selectedImage: UIImage?) {
        self.init()

        self.image = image
        self.selectedImage = image
    }

    override init() {
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

然后使用您的自定义类初始化视图控制器的tabBarItem

tabBarItem = MyTabBarItem(image: UIImage(named: "my_img"), selectedImage: nil)

答案 4 :(得分:0)

你可以试试这个。

在您不想要标题的视图控制器中添加它

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.title = ""
  }

答案 5 :(得分:0)

我最近遇到了这个问题并且认为我会发布我自己的解决方案,因为我认为它非常轻,有些人可能更喜欢它。

首先,您创建了标签项(我的是UITabBarController子类,我将子控制器传递给) - 将标签项标题设置为空字符串。

(当然,此解决方案也特定于OP方案,其中您有一个带有嵌入式Nav控制器的TabbarController - 这是相对常见的。)

您可能知道,设置UIViewController的标题会导致相关标签项选择标题。

为了拥有UIViewController标题而不设置标签项标题 - 将新的UILabel添加到视图控制器的导航项标题视图

我最后在UIViewController上创建了一个扩展名,如下所示:

extension UIViewController {

    func addNavItemTitle(resourceString: String, textColor: UIColor = UIColor.white) {
        // Add title view ~ allows overriding title w/out showing it on tabBarItem
        let titleLabel = UILabel()
        titleLabel.text = NSLocalizedString(resourceString)
        titleLabel.textColor = textColor
        titleLabel.font = UIFont.viewTitle
        titleLabel.sizeToFit()

        self.navigationItem.titleView = titleLabel
    }
}

根据需要自定义字体和颜色。让标签大小本身,这样你就不必测量它或在这里设置它的框架。

我也更喜欢使用本地化字符串而不是直接设置字符串。

现在,在任何标签子控制器中,为了设置我的视图控制器标题而不选择标签项标题 - 我只需拨打addNavItemTitle(resourceString: "example.title")

好地方在viewDidLoad

您可能也注意到我对NSLocalizedString的调用缺少一个参数。那是因为我倾向于使用相同的字符串作为资源字符串。所以我创建了一个全局函数来简化调用而不重复字符串。

像这样:

public func NSLocalizedString(_ key: String) -> String {
    return NSLocalizedString(key, comment: key)
}