Contents

titleMenuProvider

A closure that generates the navigation item’s title menu.

Declaration

var titleMenuProvider: (([UIMenuElement]) -> UIMenu?)? { get set }

Mentioned in

Discussion

UIKit calls this closure to create a context menu that appears when a person taps the title of the navigation item. UIKit passes in a set of menu element suggestions that you can either use directly or modify in this closure to customize the menu.

// Use suggested menu elements directly.
navigationItem.titleMenuProvider = { suggestions in
    return UIMenu(children: suggestions)
}

// Add a custom menu element to the suggestions.
navigationItem.titleMenuProvider = { suggestions in
    var finalMenuElements = suggestions
    finalMenuElements.append(UICommand(title: "Save", 
                                       image: UIImage(systemName: "square.and.arrow.down"), 
                                      action: #selector(self.save)))
    return UIMenu(children: finalMenuElements)
}

Before displaying the title menu, UIKit validates each element in the menu you return by traversing the responder chain, starting with the navigation controller’s topViewController. For selector-based menu elements, implement your methods on topViewController or farther up in the responder chain if you want those elements to appear in the title menu. For more information, see canPerformAction(_:withSender:).

See Also

Customizing the title menu