UIMenuController 未显示
我正在尝试创建一个自定义 UIMenuController 并将其显示在我的视图中。这是我的代码:
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *listMenuItem = [[UIMenuItem alloc] initWithTitle:@"List" action:@selector(addList:)];
[menuController setMenuItems:[NSArray arrayWithObject:listMenuItem]];
[menuController setTargetRect:CGRectMake(50.0, 50.0, 0, 0) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
[listMenuItem release];
没有错误或异常,但菜单控制器不显示。
I'm trying to create a custom UIMenuController and display it in my view. Here's my code:
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *listMenuItem = [[UIMenuItem alloc] initWithTitle:@"List" action:@selector(addList:)];
[menuController setMenuItems:[NSArray arrayWithObject:listMenuItem]];
[menuController setTargetRect:CGRectMake(50.0, 50.0, 0, 0) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
[listMenuItem release];
There are no errors or exceptions, but the menu controller just doesn't show up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要做三件事:
-becomeFirstResponder
在视图或视图控制器上。-canBecomeFirstResponder
(返回YES
)。-canPerformAction:action withSender:sender
单独显示/隐藏菜单项。You need to do three things:
-becomeFirstResponder
on the view or view controller.-canBecomeFirstResponder
(returningYES
).-canPerformAction:action withSender:sender
to show/hide menu items on an individual basis.答案提到了三件事,但要挑剔的话,有六件事:
-becomeFirstResponder
将失败。userInteractionEnabled = YES
-window
属性必须与中视图的窗口相同>inView:
参数。-canBecomeFirstResponder
并返回YES
。[menu setTargetRect:inView:]
之前调用[handlerBecomeFirstResponder]
,,否则后者会失败。[menu setTargetRect:inView]
(至少一次)和[menu setMenuVisible:animated:]
。特别是上面的 1-3 点让我很感动。我想要一个自定义菜单处理程序类,该类最初是
UIResponder
,这导致-becomeFirstResponder
返回NO
;然后它是一个UIView
,失败了,然后我尝试将它设为一个UIButton
,它起作用了,但只是因为userInteractionEnabled
默认为YES
对于按钮,NO
对于UIView
。The answer mentions three things, but to be picky, there are six:
-becomeFirstResponder
fails.userInteractionEnabled = YES
-window
property must be the same as the window for the view in theinView:
argument.-canBecomeFirstResponder
and returnYES
.[handler becomeFirstResponder]
, before[menu setTargetRect:inView:]
is called, or the latter will fail.[menu setTargetRect:inView]
(at least once) and[menu setMenuVisible:animated:]
.In particular points 1-3 above got me. I wanted a custom menu handler class that was a
UIResponder
at first, which caused-becomeFirstResponder
to returnNO
; then it was aUIView
, which failed, then I tried making it aUIButton
which worked, but only becauseuserInteractionEnabled
defaults toYES
for buttons andNO
forUIView
s.仅当视图是第一响应者并且
- (BOOL)canPerformAction
方法返回YES
因此,如果您的菜单控制器是单击按钮时显示,按钮操作中的第一行应为
[self comeFirstResponder]
。注意:这里 self 是显示菜单的视图。如果您的菜单要在长按手势上显示,请将 longPressGesture 添加到
UIView
并在长按事件中写入[self comeFirstResponder];
然后按照提到的步骤操作由奥兹。
UIMenuController
is visible on any view only if the view is first responder and- (BOOL)canPerformAction
method returnsYES
Hence if your menu controller is to be shown on button click, the first line in the button action should be
[self becomeFirstResponder]
. NOTE: here self is the view which will present the menus.If your menus are to be shown on long press gesture, then add longPressGesture to the
UIView
and in the longpress event before writingwrite
[self becomeFirstResponder];
Then follow the steps mentioned by OZ.
下面是一个完整的注释工作示例...
查看子类头文件
查看子类源文件
查看控制器头文件
查看控制器源文件
在 View 类中,如果您在 canPerformAction 中单独写入返回 YES,您将看到所有默认菜单项,如相机符号、剪切、复制等。
如果您想单独显示相机之类的内容,那么
如果您想了解所有操作,
请访问 链接
The below is a full commented working example ...
View subclass header file
View subclass source file
view Controller header file
view Controller source file
In View class if u write return YES alone in canPerformAction you will see all the default menuitems like camera symbol,cut,copy etc..
if u want to show something like camera alone then
if u want to know about all the actions then
visit the link
以防万一有人在 iOS6 上特别(随机)遇到此问题:您可能需要查看与在设备上启用朗读选择(设置 -> 常规 -> 辅助功能 -> 朗读选择:打开)。我的一小部分用户无法看到自定义
UIMenuItems
,这就是原因。Just in case anyone is having this issue specifically (and randomly) with iOS6: you might want to look at this SO related to having Speak Selection enabled on the device (Settings -> General -> Accessibility -> Speak Selection: On). A small number of my users were not able to see the custom
UIMenuItems
and this was the cause.在 Swift 3.0 中 -
就我而言,我想让 VC 预先选择 TextView 中的文本,并显示一个自定义菜单,以便用户对该选择采取操作。正如 Kalle 所提到的,顺序非常重要,尤其是使
setMenuVisible
最后。在 VC 中,
viewDidLoad
:在 VC 中,当用户点击按钮时:
最后,在 TextView 的子类中:
In Swift 3.0 -
In my case I wanted to have the VC pre-select the text in a TextView and display a custom menu for the user to take action on that selection. As mentioned by Kalle, order is very important, especially making
setMenuVisible
last.In VC,
viewDidLoad
:In VC, when the user hits a button:
Finally, in the sub-class of the TextView:
可能是因为
CGRectMake(50.0, 50.0, 0, 0)
创建了一个CGRect
,其中width = 0
和height = 0
>?干杯,
安卡
maybe because
CGRectMake(50.0, 50.0, 0, 0)
creates aCGRect
withwidth = 0
andheight = 0
?cheers,
anka