0.3 C
New York
Sunday, February 23, 2025

ios – UIBarButton – icon solely on the toolbar, and title + icon within the menu


I am including a button to UIDocumentBrowserViewController like this:

class DocumentBrowserViewController: UIDocumentBrowserViewController, UIDocumentBrowserViewControllerDelegate
{
    override func viewDidLoad()
    {
        tremendous.viewDidLoad()

        let button = UIBarButtonItem(
            picture: UIImage(...),
            model: .plain,
            goal: self,
            motion: #selector(...)
        )
        
        additionalTrailingNavigationBarButtonItems.append(button)
    }
}

This works effective, besides if the app is in split-view mode on a pill. In that case, the button strikes to a menu created by UIDocumentBrowserViewController, which accommodates many different actions which are constructed into the view controller. Within the menu, my button is just an icon on the trailing aspect of the menu merchandise.

I would like so as to add a label or title to it, so it reveals up within the menu together with the icon.

I may do that along with the above code:

button.title = "Title"

Or I may do that as an alternative of any of the above code:

let button = UIBarButtonItem(
    title: "Title",
    picture: UIImage(...),
    goal: self,
    motion: #selector(...)
)

Each of those efficiently add the title, however then no icon will seem. It is going to at all times be the textual content Title, whether or not it’s on the toolbar or within the menu.

Per @matt’s suggestion beneath, I additionally tried this:

let buttonAction = UIAction(
    title: "Title",
    picture: UIImage(...)
)
{
    motion in
            
    self.doSomething()
}
        
let button = UIBarButtonItem(
    picture: UIImage(...),
    primaryAction: buttonAction
)

This has the identical final result as the unique answer – it seems as an icon on the toolbar, which is nice, however within the menu it nonetheless doesn’t have a label.

My preferrred conduct for the button is:

  • When it’s on the toolbar, it’s an icon solely.
  • When it’s moved to the menu, it’s a main label and a trailing icon, like the opposite menu gadgets.

How can I do this?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles