UIMenuController使用
2016-03-22 16:22
489 查看
定制菜单项
如果你想使用定制菜单项,下面代码比较隐晦,但非常灵活。你需要检测是否用户长按并显示菜单,而最简单的方法就是在表格单元格中使用UILongPressGestureRecognizer
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [cell addGestureRecognizer:recognizer];
为了让菜单显示,目标视图必须在 responder 链中,很多 UIKit 视图默认并无法成为一个 responder ,因此你需要之类这些视图重载 canBecomeFirstResponder 方法范围 YES
在下面例子中,我们使用定制类 TSTableViewCell 并实现了长按选择器
- (void)longPress:(UILongPressGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateBegan) { TSTableViewCell *cell = (TSTableViewCell *)recognizer.view; [cell becomeFirstResponder]; UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag"action:@selector(flag:)]; UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve"action:@selector(approve:)]; UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny"action:@selector(deny:)]; UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]]; [menu setTargetRect:cell.frame inView:cell.superview]; [menu setMenuVisible:YES animated:YES]; } } - (void)flag:(id)sender { NSLog(@"Cell was flagged"); } - (void)approve:(id)sender { NSLog(@"Cell was approved"); } - (void)deny:(id)sender { NSLog(@"Cell was denied"); }
There is only one small gotcha with
UIMenuItem: if the specified action is not implemented by your view controller, that item will not appear in the menu.
相关文章推荐
- UIPasteboard 粘贴板
- iOS开发中设置UITableViewCell选中时的颜色
- hdu1711 Number Sequence 求模式串在主串中的位置
- 开关控件 UISwitch
- Building Android Kernel for the Nexus 5 — AOSP(6.0.1)
- 在ios7及以上系统中在UINavigationBar中添加UITextField或者UISearchbar时不显示输入光杆问题及解决
- 多个模型 在 tableview
- UISegmentedControl UISlider
- UIButton中setTitleEdgeInsets和setImageEdgeInsets的使用
- uiui
- [Leetcode]Binary Tree Longest Consecutive Sequence
- No 'Access-Control-Allow-Origin' header is present on the requested resource
- Ruiy自我识人做事领悟录ing
- ios开发基础——UIImageView
- UI常用控件和属性
- 如何编译UE4的NXGI引擎
- UITextView文字怎样居上
- (转)使用Servlet3.0新特性asyncSupported=true时抛异常java.lang.IllegalStateException: Not support...
- FineUI Gird单元格编辑 保存新增的一行到数据库 LINQ
- cell的循环利用