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

7

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

Greedy answered 12/4, 2016 at 0:26 Comment(0)
T
36

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

Tuyettv answered 17/8, 2017 at 22:42 Comment(2)
Best ever solution!! πŸ‘ – Namtar
Thanks...😊 You saved my lots of time – Workshop
I
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 = ""
Ithnan answered 12/4, 2016 at 0:50 Comment(4)
Thank you man but i already did it, it's not working How hide the title from UITabBarController ? – Greedy
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. – Ithnan
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. – Ithnan
I just updated the subject i hope it's more clear now – Greedy
V
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)
Viridissa answered 20/1, 2017 at 9:6 Comment(0)
T
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.

Tourmaline answered 14/4, 2016 at 19:19 Comment(2)
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. – Greedy
your solution affects not only on title but on touchable area too – Foreland
T
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)
}
Templeton answered 6/3, 2017 at 3:50 Comment(1)
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 – Foreland
P
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)
Paraprofessional answered 28/11, 2022 at 15:44 Comment(0)
B
-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 = ""
  }
Blancablanch answered 15/12, 2016 at 6:13 Comment(1)
it doesn't work – Foreland

© 2022 - 2024 β€” McMap. All rights reserved.