ios - 在 UIBarButtonItem 上添加手势

标签 ios objective-c xcode

我已经找到了向 View 、按钮添加手势的方法,但对此一无所知:(

这就是我正在尝试做的事情。 点击加号按钮添加标签。但是点击并按住同一个加号按钮可以调出一个操作表来添加标签组或标签。问题是向 UIBarButtonItem 提供手势。

这就是我初始化按钮的方式:

[self.navigationItem setPrompt: @"Tags"];
[self.navigationItem setRightBarButtonItem: [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @selector( pressedNavRight:)] animated: YES];
[self.navigationItem setLeftBarButtonItem: [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemEdit target: self action: @selector( pressedNavLeft:)] animated: YES];

最佳答案

UIView *buttonItemView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 30)];
buttonItemView.backgroundColor = [UIColor blackColor];
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[buttonItemView addGestureRecognizer:tapGes];
UILongPressGestureRecognizer *longPressGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
[buttonItemView addGestureRecognizer:longPressGes];
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
self.navigationItem.rightBarButtonItem = barItem;

关于ios - 在 UIBarButtonItem 上添加手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31644275/

相关文章:

ios - 如何通过 USB 镜像 iOS 屏幕?

即使已获得许可,iOS 麦克风选项也不在应用程序设置中

ios - UITabBarController setSelectedViewController : only a view controller in the tab bar controller's list of view controllers can be selected

ios - 无法在 UISearchController 中为 UITextField 设置正确的背景颜色

iphone - 尝试使用 UI 自动化点击 UITableViewCell 中的 UIButton 失败,返回 "could not be tapped"

ios - 如何更改输入总线上的抽头频率?

ios - 当 UICollectionView 最初加载其数据时,我们如何获得可见的 Cell IndexPath

html - 当通过应用程序脚本部署为 Web 应用程序时,任何知道该链接的人都可以在 Google 云端硬盘中查看图像,但不会在 IOS 的网络浏览器中显示

objective-c - "Setup Pay"文本显示为 IN_APP_PAYMENT_BUTTON_SETUP Pay

xcode - 如何在 Xcode 4 中禁用索引?