Iphone 如何在UINavigationController工具栏的背景上检测触摸

Iphone 如何在UINavigationController工具栏的背景上检测触摸,iphone,ios,uinavigationcontroller,uitoolbar,Iphone,Ios,Uinavigationcontroller,Uitoolbar,我想在UINavigationController提供的UIToolbar的背景上检测触摸(或者只需轻触即可)。我尝试过的不起作用(或无法开始工作)的事情: 将UITapGestureController添加到UIToolbar-此操作失败,因为虽然它检测到点击,但它会使用触摸,因此工具栏上的按钮停止工作(因为它们没有收到触摸) 为UIBarButtonItem(UIBarButtonSystemEmittemFlexibleSpace)设置目标和操作-FlexibleSpace似乎不接受接触

我想在UINavigationController提供的UIToolbar的背景上检测触摸(或者只需轻触即可)。我尝试过的不起作用(或无法开始工作)的事情:

  • 将UITapGestureController添加到UIToolbar-此操作失败,因为虽然它检测到点击,但它会使用触摸,因此工具栏上的按钮停止工作(因为它们没有收到触摸)
  • 为UIBarButtonItem(UIBarButtonSystemEmittemFlexibleSpace)设置目标和操作-FlexibleSpace似乎不接受接触(这似乎足够公平)

我想要的是检测任何不在按钮上的触摸。鉴于我无法为UINavigationController设置工具栏(它是只读的),因此我无法替换我自己的UIToolbar子类,是否有我缺少的技巧/解决方法?

以下代码实现了这一技巧:

CGSize toolbarSize = [[[self navigationController] toolbar] frame].size;
UIControl* backgroundControl = [[UIControl alloc] initWithFrame: CGRectMake(0, 0, toolbarSize.width, toolbarSize.height)];
[backgroundControl addTarget:self action:@selector(toolbarBackgroundTap) forControlEvents:UIControlEventTouchDown];
[backgroundControl setAutoresizingMask: [[[self navigationController] toolbar] autoresizingMask]];
[[[self navigationController] toolbar] insertSubview:backgroundControl atIndex:0];
[backgroundControl release];

@B战神-感谢您在评论中提出的建议。这非常有帮助。

你不能添加一个大按钮并将其z值设置为最低值吗
UIToolbar
是一个
UIView
,因此您应该能够。