ios - 单击导航栏按钮下方的区域时,将触发导航栏按钮单击事件。

标签 ios cocoa-touch

enter image description here

我将带有自定义 View 的导航栏作为右栏按钮项。自定义 View 中有两个按钮。但是当我触摸右侧导航栏下方的区域时,它会触发按钮触摸事件。我检查了按钮的框架,并通过更改背景颜色查看所有设置完美。 我检查了默认的 iOS 应用程序,如时钟、日历、设置、笔记等,它们都有相同的行为。触摸(默认 iOS 日历应用程序的)屏幕截图中的红色矩形会触发按钮的触摸事件,我的应用程序也发生了同样的情况。

这是代码..

UIView* navigationButtonsView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 44.0f)];

UIButton *p = [UIButton buttonWithType:UIButtonTypeCustom];
[p setImage:[UIImage imageNamed:@"PBtn.png"] forState:UIControlStateNormal];
[p addTarget:self action:@selector(PButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
[p setFrame:CGRectMake(43, 0, 57, 44)];
[navigationButtonsView addSubview:p];

UIButton *o = [UIButton buttonWithType:UIButtonTypeCustom];
[o addTarget:self action:@selector(oButtonPushed:) forControlEvents:UIControlEventTouchUpInside];
[o setFrame:CGRectMake(0, 0, 38, 44)];
[o setImage:[UIImage imageNamed:@"O.png"] forState:UIControlStateNormal];
[navigationButtonsView addSubview:o];

UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:navigationButtonsView];
[musicVC.navigationItem setRightBarButtonItem: barItem animated:NO];

如何更改导航栏按钮的默认 iOS 行为?因为我在这些按钮下方有一些 UI,因此它的顶部区域没有响应。 提前致谢..

最佳答案

这是正常行为 - 两个原因:

  1. iOS 不知道您的手指有多大 - 因此它必须决定触摸的位置。在这种情况下,导航栏会覆盖表格 View 。

  2. 这可能与人机界面指南(抱歉,没有链接。Google 一下)有关,该指南规定最小可触摸区域必须为 44 像素。此行为可能是“UIBarButtonItem”试图符合这些要求。

关于ios - 单击导航栏按钮下方的区域时,将触发导航栏按钮单击事件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15341402/

相关文章:

iphone - 如何将 iPhone 摄像头的实时视频转换为灰度?

ios - 如何将存储在 iphone 中的 pdf 上传到我在 iOS 中的应用程序

ios - 在 Swift 中跨多个目标共享 Storyboard

ios - 查询 Realm 中的关系数组

ios - 如何在 iOS 的 XMPP 框架中设置资源

ios - 在 UITableViewCell 上调用 `[UIView -systemLayoutSizeFittingSize:]` 总是失败

iphone - 在不触发 touchesCancelled 的情况下滚动 UIScrollView

ios - 插入 subview 后更改导航栏中的后退按钮文本

ios - 将 distanceFromLocation 转换为 Int

ios - 动态单元格高度尺寸不起作用