Search code examples
iosswiftios13sf-symbols

UIContextMenuActionProvider puts unwanted checkmark icons on items


The problem

I was implementing UIContextMenuInteraction, and ended up with the behavior I can't explain or find fixes too. The issue as seen from the screen shot that menu items have checkmarks. This is not intended and those checkmarks added automatically. Ideally I'd like use SF Symbols, but any image I add ends up being this checkmark. Even if I set image to nil, it still adds this weird checkmark.

Additional steps taken: Reinstall SF Symbols and SF Pro, Clean build, Restart xCode / Simulator

Reproduced: Simulator iOS 13.3, iPhone 7 iOS 13.3

System: Catalina 10.15.1, xCode 11.3.1

Code:

import UIKit

class ViewController: UIViewController {

  let sampleView = UIView(frame: CGRect(x: 50, y: 300, width: 300, height: 200))

  override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(sampleView)
    sampleView.backgroundColor = .systemIndigo


    let interaction = UIContextMenuInteraction(delegate: self)
    sampleView.addInteraction(interaction)
  }
}

extension ViewController: UIContextMenuInteractionDelegate {

  func contextMenuInteraction(
    _ interaction: UIContextMenuInteraction,
    configurationForMenuAtLocation location: CGPoint
  ) -> UIContextMenuConfiguration? {

    let actionProvider: UIContextMenuActionProvider = { [weak self] _ in

      let like = UIAction(
        title: "Like",
        image: UIImage(systemName: "heart"),
        identifier: nil,
        discoverabilityTitle: nil,
        attributes: [],
        state: .on
      ) { _ in

      }

      let copy = UIAction(
        title: "Copy",
        image: nil,
        identifier: nil,
        discoverabilityTitle: nil,
        attributes: [],
        state: .on
      ) { _ in

      }

      let delete = UIAction(
        title: "Delete",
        image: UIImage(systemName: "trash"),
        identifier: nil,
        discoverabilityTitle: nil,
        attributes: [.destructive],
        state: .on
      ) { _ in

      }

      return UIMenu(
        title: "",
        image: nil,
        identifier: nil,
        options: [],
        children: [
          like, copy, delete
        ]
      )
    }


    let config = UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: actionProvider)

    return config

  }

}

Weird checkmarks


Solution

  • You need to change UIAction.state from .on to .off to get rid of the checkmark.