'UIBarButtonItem Initializer with Image and Title does not show Title

I am using init(title:image:primaryAction:menu:) of a UIBarButtonItem Apple Documentation on a Toolbar with the hope of showing a button with both image and title. Does anyone know why the title does not show? I am not looking for a custom implementation that can add a UIBarButtonItem with title and image. I have seen plenty on Stackoverflow. I just want to use this initializer if it works.

I have a UITableViewController subclass and I have added this code to the viewDidLoad

navigationController?.isToolbarHidden = false
let barButtonItem = UIBarButtonItem(title: "New Item", image: UIImage(systemName: "plus.circle.fill"), primaryAction: nil, menu: nil)
toolbarItems = [barButtonItem]


Solution 1:[1]

Try to assign a custom button to your navBar button, declare your button under your controller class:

let myButton = UIButton(type: .system)

now in viewDidLoad set your custom button with title and assign it to navigation bar:

myButton.setImage(UIImage(systemName: "square.and.arrow.up"), for: .normal)
myButton.setTitle(" your text", for: .normal)
myButton.sizeToFit()
myButton.addTarget(self, action: #selector(yourFunctionAction), for: .touchUpInside)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: myButton)

Solution 2:[2]

My guess is that since public convenience init(title: String? = nil, image: UIImage? = nil, primaryAction: UIAction? = nil, menu: UIMenu? = nil) is a convenience init, and the fact that there is no init that initialize with both title and image, then using both at the same time is not supported. It's merely a convenience on using primaryAction and menu then having title and image.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fabio
Solution 2 CiNN