错误的 UISearchBar 触摸区域

Wrong UISearchBar touches area

总结

从不带状态栏的视图控制器推送带状态栏的视图控制器后,UISearchBar 触摸区域出现错误。

说明

我在 UINavigationController 里面有两个视图控制器 A 和 B。使用以下代码隐藏状态栏:

- (BOOL)prefersStatusBarHidden {
    return YES;
}

B 在 UINavigationController 标题视图中有 UISearchBar。在此视图中启用状态栏:

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

从A推送B后似乎没有什么问题:

但是当我尝试按下测试按钮或状态栏时,触摸识别错误:

当我按下导航栏下方的红色区域时,搜索栏和后退按钮会激活。

视频:https://www.youtube.com/watch?v=Jw4kDOFBCIg

小示例项目:https://github.com/leo150/SearchBarTest

有什么想法吗?谢谢

这其实是apple实现的bug或者feature。导航栏正下方的按钮总是有问题。

Apple 可能这样做了,因此可以更轻松地点击更突出的 UI 部分(导航栏)。

正如 Alistra 所说,这是 Apple 为改善用户体验而实施的。

这是一个相关的线程,有一些解决方法: Why does UINavigationBar steal touch events?

还有苹果工程师的official answer

I recommend that you avoid having touch-sensitive UI in such close proximity to the nav bar or toolbar. These areas are typically known as "slop factors" making it easier for users to perform touch events on buttons without the difficulty of performing precision touches. This is also the case for UIButtons for example.

But if you want to capture the touch event before the navigation bar or toolbar receives it, you can subclass UIWindow and override: -(void)sendEvent:(UIEvent *)event;