Note: xcode is Version 11.4.1 (11E503a)
I have created customized UITextView so that to add additional menus and tried to disable several default UIMenuItems, but I couldn't find a way to do so. The code is like below:
class MyUITextViewController: UIViewController {
var textView: UITextView! = UITextView()
override func viewDidLoad() {
super.viewDidLoad()
textView.isScrollEnabled = true
textView.isEditable = true
textView.isUserInteractionEnabled = true
view.addSubview(textView)
UIMenuController.shared.menuItems = [
UIMenuItem(title: "My Menu", action: #selector(myMenu))
]
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textView.frame = view.frame
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == Selector(("_lookup:")) {
var responder: UIResponder? = self.next
while responder != nil {
print("responder = \(responder!)")
print("canPerformAction = \(responder!.canPerformAction(action, withSender: sender))")
responder = responder?.next
}
}
return action == #selector(myMenu)
}
@objc
func myMenu() {
print("my menu")
}
}
struct MyTextViewController: UIViewControllerRepresentable {
@Binding var text: String
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> MyUITextViewController {
let viewController = MyUITextViewController()
viewController.textView.delegate = context.coordinator
return viewController
}
func updateUIViewController(_ vc: MyUITextViewController, context: Context) {
if vc.textView.text != text {
vc.textView.text = text
}
}
class Coordinator: NSObject, UITextViewDelegate {
var parent: MyTextViewController
init(_ textView: MyTextViewController) {
self.parent = textView
}
func textViewDidChange(_ textView: UITextView) {
self.parent.text = textView.text
}
}
}
struct MyView: View {
@State var text = ""
var body: some View {
MyTextViewController(text: $text)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
canPerformAction returns false except for my menu, but there still be other menus such as select, paste, look up, learn, share, etc.
So I debugged to find out which responder returns true for for example lookup menu, and it dumps below log:
responder = <_TtGC7SwiftUI16PlatformViewHostGVS_42PlatformViewControllerRepresentableAdaptorV8clipping20MyTextViewController__: 0x105097b00; frame = (0 20; 375 647); anchorPoint = (0, 0); tintColor = UIExtendedSRGBColorSpace 0 0.478431 1 1; layer = >
canPerformAction = false
responder = <_TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentV8clipping6MyViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_15EnvironmentData____: 0x1050882a0; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = ; layer = >
canPerformAction = true
responder = <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentV8clipping6MyViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_15EnvironmentData____: 0x105083cf0>
canPerformAction = false
responder = >
canPerformAction = false
responder = >
canPerformAction = false
responder = ; layer = >
canPerformAction = false
responder = ; persistentIdentifier = 6942827B-F542-40EF-8B48-57719BE39C64; activationState = UISceneActivationStateForegroundActive; settingsCanvas = ; windows = (
"; layer = >",
">"
)>
canPerformAction = false
responder =
canPerformAction = false
responder =
canPerformAction = false
I am not sure what_TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentV8clipping6MyViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_15EnvironmentData____ is exactly which class, but it contains UIHostingView so I doubt that UIHostingVIew, which seems view of UIHostingController, responds lookup action should be enabled.
Is there any way to disable those default menus? Is it able to customize canPerformAction of UIHostingView?