UIMenuController 的创建
UIMenuController *menu = [UIMenuController sharedMenuController];
// 设置菜单内容
menu.menuItems = @[
[[UIMenuItem alloc] initWithTitle:@"顶" action:@selector(ding)],
[[UIMenuItem alloc] initWithTitle:@"撤销" action:@selector(del)]
];
CGFloat maxY = CGRectGetMaxY(self.contentLabel.frame)+5;
// 显示位置
CGRect rect = CGRectMake(0, maxY, 160, 60);
[menu setTargetRect:rect inView:self];
// 显示出来
[menu setMenuVisible:YES animated:YES];
关键是即使UIMenuController在当前控制器子视图中
下面 这两个方法必须写到当前控制器中。才能正常显示
// 用于UIMenuController显示,缺一不可
-(BOOL)canBecomeFirstResponder{
return YES;
}
// 用于UIMenuController显示,缺一不可
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if (action ==@selector(ding) || action ==@selector(del)){
return YES;
}
return NO;//隐藏系统默认的菜单项
}