简介
Displaying and Managing the Edit Menu 官方文档
管理选择和编辑菜单
- 触发显示编辑菜单,注意需要能成为第一响应者才行,看情况是否需要继承处理。
- (void)tapHandler {
if (![self becomeFirstResponder]) {
NSLog(@"error");
return;
}
UIMenuController *menu=[UIMenuController sharedMenuController];
[menu setTargetRect:self.bounds inView:self];
[menu setMenuVisible:YES];
}
- 选择性显示
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(cut:)) {
return YES;
}
return NO;
}
添加自定义项目
- 一般是UIResponder类的子类即可,比如继承UIImage,注意需要使用点击或者其他方式触发
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = YES;
UITapGestureRecognizer *pressGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHandler)];
[self addGestureRecognizer:pressGesture];
}
return self;
}
- 必须第一响应者才会触发编辑菜单
- (BOOL)canBecomeFirstResponder {
return YES;
}
- 触发显示编辑菜单
- (void)tapHandler {
if (![self becomeFirstResponder]) {
NSLog(@"error");
return;
}
UIMenuController *menu=[UIMenuController sharedMenuController];
// 自定义的菜单,注意seletor需要选择在后面显示
UIMenuItem *pop = [[UIMenuItem alloc]initWithTitle:@"Pop" action:@selector(popSelf)];
menu.menuItems=@[pop];
[menu setTargetRect:self.bounds inView:self];
menu.arrowDirection=UIMenuControllerArrowDown;
[menu update];
[menu setMenuVisible:YES];
}
- 选择菜单按钮显示
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if (action == @selector(popSelf)) {�
return YES;
}
return NO;
}
- 自定义的菜单方法
- (void)popSelf {
NSLog(@"pop");
}
隐藏编辑菜单
- 默认自动隐藏。
- 关于编辑菜单,还有额外的通知可供使用。
网友评论