[英]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):
我尝试过的东西不起作用(或者我无法上班):
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.