我正在尝试让UIMenuController显示一个菜单。
以下是我到目前为止拥有的代码:
commentCell.userInteractionEnabled = YES;
[commentCell becomeFirstResponder];
UIMenuController *menu = [UIMenuController sharedMenuController];
if (menu.menuVisible) {
return;
}
menu.menuItems = @[[[UIMenuItem alloc] initWithTitle:@"copy" action:@selector(log)], [[UIMenuItem alloc] initWithTitle:@"report" action:@selector(log)]];
const CGRect targetFrame = commentCell.frame;
const CGRect convertedFrame = [commentCell convertRect:targetFrame toView:self.viewController.view];
[menu setTargetRect:convertedFrame inView:self.viewController.view];
[menu update];
[menu setMenuVisible:YES animated:YES];不幸的是,我的菜单似乎没有显示出来。我试图遵循UIMenuController not showing up中概述的指导原则,但我已经尝试了所有的建议,包括使我的视图实现canBecomeFirstResponder和将userInteractionEnabled设置为YES。有什么想法可能是这个问题,或者我可以如何进一步调试?
发布于 2018-11-08 06:31:12
找到了问题所在。原来我还需要包含字段:self.viewController中的-(BOOL)canPerformAction
发布于 2018-11-08 07:11:55
我想添加一个问题,即targetFrame应该是:
const CGRect targetFrame = commentCell.bounds;
https://stackoverflow.com/questions/53198670
复制相似问题