How to hide a title in a tab bar item using the navigation controller

I have a tab item on the navigation controller connected to the tab bar controller and I want to remove the title fast

Tab bar controller> Navigation controller> View controller

Tab bar item

Program stream

The application starts with a tab bar controller with five tabs, each of which works fine. I mean hiding the title under the tab bar item, but on the tab in the image there is only a problem that was not hidden, and for this the application also works on this tab in order if the user is logged out and the Viewcontroller is displayed on the image, but if the user signs the title in the tab bar, it is displayed so that if you are absent, I can hide the title programmatically

+4
source share
6 answers

Xcode , , . tabBarItem . barItem.

, , .

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

UITabBarItem, . , title = "Booking" BookingViewController.

API, , , , , :

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

, , tabBarItem.imageInsets.

+2

, Interface Builder, , .

, title UIViewController, . , navigationItem.title = "My Title" title = "My Title".

+2

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)
+1

.

, .

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

, , , , .

-, ( - UITabBarController, ) - .

(, OP, TabbarController Nav Controller, ).

, , , UIViewController , .

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

, . , .

.

, , , Tab, addNavItemTitle(resourceString: "example.title")

viewDidLoad

You may also have noticed that my call is NSLocalizedStringmissing an argument. This is because I tend to use the same line for comment as the resource line. So I created a global function to simplify the call without repeating the line.

Same:

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

All Articles