简体   繁体   English

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

[英]how to detect touches on the background of UINavigationController toolbar

I'd like to detect touches (or just a tap would do) on the background to the UIToolbar supplied by a UINavigationController. 我想在UINavigationController提供的UIToolbar的背景上检测触摸(或者只是点击一下)。 Things I have tried that don't work (or that I couldn't get to work): 我尝试过的东西不起作用(或者我无法上班):

  • adding a UITapGestureController to the UIToolbar - this fails because although it does detect the tap, it consumes touches so the buttons on the toolbar stop working (as they receive no touches) 将UITapGestureController添加到UIToolbar - 这会失败,因为虽然它确实检测到了点击,但它会消耗触摸因此工具栏上的按钮停止工作(因为它们没有接触到触摸)
  • setting a target and action on a UIBarButtonItem that is a UIBarButtonSystemItemFlexibleSpace - flexible spaces don't appear to pick up touches (which seems fair enough) 在作为UIBarButtonSystemItemFlexibleSpace的UIBarButtonItem上设置目标和操作 - 灵活的空间似乎没有接受触摸(这看起来很公平)

What I want is to detect any touch that is not on a button. 我想要的是检测任何不在按钮上的触摸。 Given that I can't set the toolbar for the UINavigationController (it is read only), so I can't substitute my own UIToolbar subclass, is there a trick / workaround that I am missing? 鉴于我无法为UINavigationController设置工具栏(它是只读的),所以我无法替换我自己的UIToolbar子类,是否有一个我缺少的技巧/解决方法?

The following code does the trick: 以下代码可以解决问题:

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];

@bdares - thanks for the suggestion in your comment. @bdares - 感谢您在评论中提出的建议。 That was very helpful. 这非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM