checkmark on a submenu option within UIMenu in Swift

Viewed 708

I have the following code that generates a context menu when a user taps on a uibarbutton in a navigation bar:

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGreen
        title = "Hello"
        
        let scribbleAction =  UIAction(title: "Scribble", image: UIImage(systemName: "scribble"), state: .on, handler: { (_) in })
        let textAction =  UIAction(title: "Typing", image: UIImage(systemName: "textformat"), state: .off, handler: { (_) in })
        
        let clockAction = UIAction(title: "Show Clock", image: UIImage(systemName: "clock"), state: .off, handler: { (_) in })
        let calendarAction = UIAction(title: "Date Last Used", image: UIImage(systemName: "calendar"), state: .off, handler: { (_) in })
        
        var showAsActions: [UIAction] {
            return [
                clockAction, calendarAction
            ]
        }
        
        var showAsMenu : UIMenu {
            return UIMenu(title: "Show As", image: UIImage(systemName: "questionmark.circle.fill"), identifier: .none, children: showAsActions)
        }
    
        let menu = UIMenu(title: "",
                          image: nil,
                          identifier: nil,
                          options: [],
                          children: [showAsMenu, scribbleAction, textAction])
                
        let barButtonRight = UIBarButtonItem(title: "Sort", image: UIImage(systemName: "ellipsis.circle.fill"), primaryAction: nil, menu: menu)
        navigationItem.rightBarButtonItems = [barButtonRight]
    }
}

This all works and produces the following when a user taps on the button in the navigation bar:

enter image description here

Tapping on the first menu item yields the desired effect:

enter image description here

But at times, I need to be able to also mark the first menu item (which is a submenu item) with a checkmark. I can't work out how to do this and have searched on Stackoverflow as well as the Apple Developer Forums etc and can't work it out. Any suggestions would be greatly appreciated.

0 Answers
Related