11

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

SALEH
  • 113
  • 1
  • 1
  • 7

7 Answers7

33

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".

Peter
  • 766
  • 7
  • 8
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 = ""
Xyz
  • 5,955
  • 5
  • 40
  • 58
caiomcg
  • 501
  • 10
  • 23
  • Thank you man but i already did it, it's not working How hide the title from UITabBarController ? – SALEH Apr 12 '16 at 02:16
  • Well, the xcode trick should have worked fine. It just set the text that appears below the item. If you want to hide and make it appear again you will have to use the programmatic code. – caiomcg Apr 12 '16 at 02:21
  • I will assume you have asked me where to add the code. Well, in any view linked to the tabbar, but you should add to the first view that appears. – caiomcg Apr 12 '16 at 11:09
  • I just updated the subject i hope it's more clear now – SALEH Apr 14 '16 at 18:55
2

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)
Emad
  • 669
  • 8
  • 21
1

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.

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • Thank you man i got it , thanks to your idea about overridden i just find that my viewcontroller has a title on in the navigation controller that is effecting my title on tab bar item. – SALEH Apr 15 '16 at 01:48
  • your solution affects not only on title but on touchable area too – Gargo Feb 28 '23 at 05:22
1

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)
}
Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
Bladebunny
  • 1,251
  • 9
  • 12
  • according to single responsibility principle you should call `NSLocalizedString` somewhere else and pass to `addNavItemTitle` the string which is already localized. But anyways thanks for a solution – Gargo Feb 28 '23 at 05:31
0

Change the title color of the selected item to the tab bar back ground color

viewController.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .selected)
Aalaa
  • 55
  • 6
-1

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 = ""
  }
Umesh Verma
  • 876
  • 7
  • 28