You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在iOS 16(Xcode14 Beta)中移除选中文本的“搜索网页”选项

iOS 16 UITextField 移除“搜索网页”菜单选项解决方案

重写canPerformAction(_:withSender:)返回false只能移除复制、粘贴这类常规编辑菜单,但iOS 16新增的“搜索网页”选项不受该方法控制,需要通过重写buildMenu(with:)方法来处理:

方案一:移除整个查询类菜单组

如果需要移除包括“搜索网页”在内的所有查询类菜单,可以直接移除.lookup菜单组:

class CustomUITextField: UITextField {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
    
    override func buildMenu(with builder: UIMenuBuilder) {
        super.buildMenu(with: builder)
        // 移除iOS16+新增的查询类菜单(包含搜索网页)
        builder.remove(menu: .lookup)
    }
}

方案二:精准移除“搜索网页”选项

如果只想移除“搜索网页”而保留其他查询类菜单,可以通过action的identifier精准定位:

class CustomUITextField: UITextField {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
    
    override func buildMenu(with builder: UIMenuBuilder) {
        super.buildMenu(with: builder)
        guard let lookupMenu = builder.menu(for: .lookup) else { return }
        // 遍历查询菜单下的所有action,移除搜索网页对应的选项
        lookupMenu.children.compactMap { $0 as? UIAction }.forEach { action in
            if action.identifier == .lookupWeb {
                builder.remove(action: action)
            }
        }
    }
}

原理:iOS 16开始,“搜索网页”属于系统的.lookup菜单组,这类菜单的创建逻辑不再依赖canPerformAction(_:withSender:),而是通过UIMenuBuilder来构建,因此需要通过菜单构建相关的方法来移除。

内容的提问来源于stack exchange,提问作者user15748784

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 17:48:20