使用 UIEditMenu 与 WebKit 交互

问题描述 投票:0回答:3

在 WKWebview 上为所选文本呈现自定义菜单时,我使用 UIMenuController。但现在在 iOS 16 上已弃用,并出现以下错误

[Text] Using UIMenuController to add items into text menus is deprecated. Please implement the UITextInput API editMenuForTextRange:suggestedActions: instead.

[EditMenuInteraction] The edit menu ... did not have performable commands and/or actions; ignoring present.

现在我找不到任何关于如何在 wkwebview 上自定义菜单的文档。

这就是我想在菜单上呈现的内容。

如何在wkwebview上自定义所选文本上的菜单?

我尝试添加 UITextInput,但它需要遵守一堆协议。

ios swift wkwebview ios16 uimenucontroller
3个回答
2
投票

我在 iOS 16 中的 Web 视图中呈现自定义菜单时遇到了同样的问题。 我实现了以下方法并删除了所有不需要的菜单项,如下所示,一切顺利:

    open override func buildMenu(with builder: UIMenuBuilder) {
    if #available(iOS 16.0, *) {
        builder.remove(menu: .lookup)
        builder.remove(menu: .file)
        builder.remove(menu: .edit)
        builder.remove(menu: .view)
        builder.remove(menu: .window)
        builder.remove(menu: .help)
        builder.remove(menu: .about)
        builder.remove(menu: .preferences)
        builder.remove(menu: .services)
        builder.remove(menu: .hide)
        builder.remove(menu: .quit)
        builder.remove(menu: .newScene)
        builder.remove(menu: .openRecent)
        builder.remove(menu: .close)
        builder.remove(menu: .print)
        builder.remove(menu: .document)
        builder.remove(menu: .undoRedo)
        builder.remove(menu: .standardEdit)
        builder.remove(menu: .find)
        builder.remove(menu: .replace)
        builder.remove(menu: .share)
        builder.remove(menu: .textStyle)
        builder.remove(menu: .spelling)
        builder.remove(menu: .spellingPanel)
        builder.remove(menu: .spellingOptions)
        builder.remove(menu: .substitutions)
        builder.remove(menu: .substitutionsPanel)
        builder.remove(menu: .substitutionOptions)
        builder.remove(menu: .transformations)
        builder.remove(menu: .speech)
        builder.remove(menu: .learn)
        builder.remove(menu: .format)
        builder.remove(menu: .font)
        builder.remove(menu: .textSize)
        builder.remove(menu: .textColor)
        builder.remove(menu: .textStylePasteboard)
        builder.remove(menu: .text)
        builder.remove(menu: .writingDirection)
        builder.remove(menu: .alignment)
        builder.remove(menu: .toolbar)
        builder.remove(menu: .sidebar)
        builder.remove(menu: .fullscreen)
        builder.remove(menu: .minimizeAndZoom)
        builder.remove(menu: .bringAllToFront)

    }
    super.buildMenu(with: builder)
}

希望这有帮助,干杯


1
投票

这目前还不可能,但我认为苹果计划很快为

UIEditMenuInteraction
添加API。这是我在 WebKit 源 中找到的内容(注意
WK_IOS_TBA
可用性说明符):

/**
 * @abstract Called when the web view is about to present its edit menu.
 *
 * @param webView The web view displaying the menu.
 * @param animator Appearance animator. Add animations to this object to run them alongside the appearance transition.
 */
- (void)webView:(WKWebView *)webView willPresentEditMenuWithAnimator:(id<UIEditMenuInteractionAnimating>)animator WK_API_AVAILABLE(ios(WK_IOS_TBA));

/**
 * @abstract Called when the web view is about to dismiss its edit menu.
 *
 * @param webView The web view displaying the menu.
 * @param animator Dismissal animator. Add animations to this object to run them alongside the dismissal transition.
 */
- (void)webView:(WKWebView *)webView willDismissEditMenuWithAnimator:(id<UIEditMenuInteractionAnimating>)animator WK_API_AVAILABLE(ios(WK_IOS_TBA));

更新:这些方法现在也出现在

WKUIDelegate
文档中:


0
投票

在包含

WKWebView
控件的视图控制器中,重写
buildMenu
方法以添加自定义上下文菜单项,如下所示:

override func buildMenu(with builder: any UIMenuBuilder) {
     
    let customMenuItem = UIAction(title: "Custom Title", image: UIImage(systemName: "star")) { action in
        // Handle menu item action here
    }
    
    let menu = UIMenu(title: String(), image: nil, identifier: nil, options: .displayInline, children: [customMenuItem])
    builder.insertSibling(menu, afterMenu: .standardEdit) // Or any other system menu
    
    super.buildMenu(with: builder)
}

适用于 iOS 16 或更高版本

© www.soinside.com 2019 - 2024. All rights reserved.