9
votes

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

6 Answers

25
votes

As others have suggested, you can either go to Interface Builder and erase the Title on the Bar Item or you can do it programatically.

This is enough as long as you don't set the title property for the UIViewController that your tab links to. If you want to set a title for your view controller and avoid it showing up as the bar item title, use navigationItem.title = "My Title" instead of title = "My Title".

4
votes

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
votes

Tabs can be configured with a title on the UITabBarItem, which is overridden by the title of the root view controller in the tab. So I imagine you are setting title = "Booking" in BookingViewController.

There is no API I know of to explicitly hide tab titles, but you can hide them by basically moving them off screen:

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

Then your icons may appear a little high, which you can also adjust to compensate by setting tabBarItem.imageInsets.

2
votes

I'd create a subclass of UITabBarItem that limits setting title like this:

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

Then initialize your view controller's tabBarItem using your custom class:

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

you can try this.

add this in view controllers that you dont want title

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

I recently came across this question and thought I'd post my own solution, for posterity, as I think its pretty light-weight and some may prefer it.

First, however you create your tab items (mine is a UITabBarController subclass that I pass my child controllers into) - set the tab item title to an empty string.

(Of course, this solution is also specific to the OP scenario where you have a TabbarController with an embedded Nav Controller - which is relatively common).

As you probably know, setting the UIViewController's title will cause the related tab item to pick up the title.

In order to have a UIViewController title without setting the Tab item title -- add a new UILabel to the view controller's Navigation Item Title View.

I wound up creating an extension on UIViewController like so:

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

Customize your fonts and colors however you like. Let the label size itself so you don't have to measure it or set its frame here.

I also prefer to use localized strings vs. setting the strings directly.

Now, in any tab child controller, in order to set my view controller title while not picking up a Tab item title - I just call addNavItemTitle(resourceString: "example.title")

A good place is in viewDidLoad

You may have also noticed that my call to NSLocalizedString is missing an argument. That's because I tend to use the same string for the comment as the resource string. So I've created a global function to simplify the call without repeating the string.

Like so:

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