如何删除 Swift 中的 UIMenuController 默认项
How to Remove UIMenuController Default Items In Swift
我正在尝试删除项目 查找 & 分享... 来自 UIMenuController。我将如何专门删除这两个并保留我的自定义一个。这是我到目前为止取得的成就:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// add two custom menu items to the context menu of UIWebView (assuming in contenteditable mode)
let menuItem1 = UIMenuItem(title: "My Button", action: #selector(myButtonSel))
UIMenuController.shared.menuItems = [menuItem1]
}
这是我的 canPerformAction:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
//let shareSelector: Selector = NSSelectorFromString("_share:")
if webView?.superview != nil {
if action == #selector(myButtonSel){
return true
}
}
return super.canPerformAction(action, withSender: sender)
}
同样出于一些奇怪的原因,当我尝试删除所有默认项目并只保留我的自定义项时,它不起作用。这是我为此尝试的代码:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
//let shareSelector: Selector = NSSelectorFromString("_share:")
if webView?.superview != nil {
if action == #selector(myButtonSel){
return true
}
else {
return false
}
}
return super.canPerformAction(action, withSender: sender)
}
即使我尝试删除所有其他项目并保留我的习惯,我也无法这样做。我所能做的就是添加我的自定义项。
我尝试了这个,但它通过子类化 WebView 并覆盖 canPerformAction
方法对我有用,我在其中手动删除了默认选项。
override func canPerformAction(_ action: Selector, withSender sender: AnyObject?) -> Bool {
if action == #selector(cut(_:)) {
return false
}
if action == #selector(paste(_:)) {
return false
}
if action == #selector(select(_:)) {
return false
}
if action == #selector(selectAll(_:)) {
return false
}
...
return super.canPerformAction(action, withSender: sender)
}
我参考了 Ike10 的 回答,它对我有用。试一试。
Objective C 从 UIMenuController 中删除默认项的版本。默认项目是 UIResponderStandardEditActions 的一部分!要删除默认项,请确保创建 UITextField 或 UITextView 的子 class 以使该功能正常工作,否则在 UiViewController class 中将无法正常工作。
#import "CustomTextField.h"
@implementation CustomTextField
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(captureTextFromCamera:) ||
action == @selector(delete:) ||
action == @selector(cut:) ||
[NSStringFromSelector(action) isEqualToString:@"_promptForReplace:"] ||
[NSStringFromSelector(action) isEqualToString:@"_transliterateChinese:"] ||
[NSStringFromSelector(action) isEqualToString:@"_insertDrawing:"] ||
[NSStringFromSelector(action) isEqualToString:@"_lookup:"] ||
[NSStringFromSelector(action) isEqualToString:@"_define:"] ||
[NSStringFromSelector(action) isEqualToString:@"_translate:"] ||
[NSStringFromSelector(action) isEqualToString:@"_addShortcut:"] ||
[NSStringFromSelector(action) isEqualToString:@"_accessibilitySpeak:"] ||
[NSStringFromSelector(action) isEqualToString:@"_accessibilitySpeakLanguageSelection:"] ||
[NSStringFromSelector(action) isEqualToString:@"_share:"] )
{
return false;
}
NSLog(@"OPtion :- %@",NSStringFromSelector(action));
return [super canPerformAction:action withSender:sender];
}
@end
默认可用选项低于您要禁用的选项。
cut:
copy:
paste:
delete:
_promptForReplace:
_transliterateChinese:
_insertDrawing:
captureTextFromCamera:
_showTextStyleOptions:
_lookup:
_define:
_translate:
_addShortcut:
_accessibilitySpeak:
_accessibilitySpeakLanguageSelection:
_accessibilityPauseSpeaking:
_share:
makeTextWritingDirectionRightToLeft:
makeTextWritingDirectionLeftToRight:
swift 用户请转换。很容易转换。
我正在尝试删除项目 查找 & 分享... 来自 UIMenuController。我将如何专门删除这两个并保留我的自定义一个。这是我到目前为止取得的成就:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// add two custom menu items to the context menu of UIWebView (assuming in contenteditable mode)
let menuItem1 = UIMenuItem(title: "My Button", action: #selector(myButtonSel))
UIMenuController.shared.menuItems = [menuItem1]
}
这是我的 canPerformAction:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
//let shareSelector: Selector = NSSelectorFromString("_share:")
if webView?.superview != nil {
if action == #selector(myButtonSel){
return true
}
}
return super.canPerformAction(action, withSender: sender)
}
同样出于一些奇怪的原因,当我尝试删除所有默认项目并只保留我的自定义项时,它不起作用。这是我为此尝试的代码:
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
//let shareSelector: Selector = NSSelectorFromString("_share:")
if webView?.superview != nil {
if action == #selector(myButtonSel){
return true
}
else {
return false
}
}
return super.canPerformAction(action, withSender: sender)
}
即使我尝试删除所有其他项目并保留我的习惯,我也无法这样做。我所能做的就是添加我的自定义项。
我尝试了这个,但它通过子类化 WebView 并覆盖 canPerformAction
方法对我有用,我在其中手动删除了默认选项。
override func canPerformAction(_ action: Selector, withSender sender: AnyObject?) -> Bool {
if action == #selector(cut(_:)) {
return false
}
if action == #selector(paste(_:)) {
return false
}
if action == #selector(select(_:)) {
return false
}
if action == #selector(selectAll(_:)) {
return false
}
...
return super.canPerformAction(action, withSender: sender)
}
我参考了 Ike10 的
Objective C 从 UIMenuController 中删除默认项的版本。默认项目是 UIResponderStandardEditActions 的一部分!要删除默认项,请确保创建 UITextField 或 UITextView 的子 class 以使该功能正常工作,否则在 UiViewController class 中将无法正常工作。
#import "CustomTextField.h"
@implementation CustomTextField
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(captureTextFromCamera:) ||
action == @selector(delete:) ||
action == @selector(cut:) ||
[NSStringFromSelector(action) isEqualToString:@"_promptForReplace:"] ||
[NSStringFromSelector(action) isEqualToString:@"_transliterateChinese:"] ||
[NSStringFromSelector(action) isEqualToString:@"_insertDrawing:"] ||
[NSStringFromSelector(action) isEqualToString:@"_lookup:"] ||
[NSStringFromSelector(action) isEqualToString:@"_define:"] ||
[NSStringFromSelector(action) isEqualToString:@"_translate:"] ||
[NSStringFromSelector(action) isEqualToString:@"_addShortcut:"] ||
[NSStringFromSelector(action) isEqualToString:@"_accessibilitySpeak:"] ||
[NSStringFromSelector(action) isEqualToString:@"_accessibilitySpeakLanguageSelection:"] ||
[NSStringFromSelector(action) isEqualToString:@"_share:"] )
{
return false;
}
NSLog(@"OPtion :- %@",NSStringFromSelector(action));
return [super canPerformAction:action withSender:sender];
}
@end
默认可用选项低于您要禁用的选项。
cut:
copy:
paste:
delete:
_promptForReplace:
_transliterateChinese:
_insertDrawing:
captureTextFromCamera:
_showTextStyleOptions:
_lookup:
_define:
_translate:
_addShortcut:
_accessibilitySpeak:
_accessibilitySpeakLanguageSelection:
_accessibilityPauseSpeaking:
_share:
makeTextWritingDirectionRightToLeft:
makeTextWritingDirectionLeftToRight:
swift 用户请转换。很容易转换。