iOS 16 PDFView highlight button doing nothing & can't override

22 Views Asked by At

I'm trying to make a custom highlight functionality in PDFKit's PDFView on iOS 16. I've tried everything recommended online, and it seems like there's no way to fully override the menu and remove the normal "Highlight" action which actually does nothing if you tap it.

I'd be okay with being able to either remove the base Highlight action because I'm able to add my own, or figure out how to handle a tap on that button. Here's what I've tried, and the result: screenshot of result

override func buildMenu(with builder: UIMenuBuilder) {
        super.buildMenu(with: builder)

        builder.remove(menu: .application)
        builder.remove(menu: .lookup)
        builder.remove(menu: .learn)
        builder.remove(menu: .find)
        builder.remove(menu: .file)
        builder.remove(menu: .edit)
        builder.remove(menu: .services)

        if #available(iOS 16.0, *) {
            builder.remove(menu: .document)
        }

        let highlightAction = UIAction(title: "Highlight 2", attributes: []) { _ in
                 print("This works")
            }
        }

        let highlightMenu = UIMenu(title: "Highlight 2", options: [.displayInline], children: [highlightAction])

        builder.remove(menu: .standardEdit)
        builder.remove(menu: .share)
        builder.remove(menu: .toolbar)
        builder.remove(menu: .text)
        builder.remove(menu: .font)
        builder.remove(menu: .format)
        builder.remove(menu: .transformations)
        builder.remove(menu: .preferences)
        builder.remove(menu: .window)
        builder.remove(menu: .view)
        
        builder.replaceChildren(ofMenu: .root, from: { _ in [highlightAction] })
    }
1

There are 1 best solutions below

0
olekstu On

Don't use builder.replaceChildren, instead use this initializer to create the childMenu: https://developer.apple.com/documentation/uikit/uimenu/3358594-init

Then, use

builder.insertChild(childMenu, .atStartOfMenu: .lookup)

doc: https://developer.apple.com/documentation/uikit/uimenubuilder/3327304-insertchild