为什么在触摸边界之外触摸时,导航项自定义标题视图的UITextField会聚焦?

时间:2016-08-31 08:11:54

标签: uitextfield uitouch

我在navigationitem自定义标题视图上有一个UITextFields。当我触摸它的右侧时,它也会得到关注。我用hittest方法测试情况。这是数据。

hitbounds field point: {269, 6.5}, bounds:{{0, 0}, {255, 35}}; searchView point: {59, 6.5}, bounds: {{0, 0}, {45, 35}}; searchButton: {58, 11}, bounds: {{0, 0}, {44, 44}}

字段视图在右侧包含一个包含搜索按钮的搜索视图。

字段点:{269,6.5},边界:{{0,0},{255,35}};
关键不在于它的界限。但是这个领域变得很糟糕,键盘出现了。

最终我发现当键盘出现时,方法甚至不会被称为pointInside:(CGPoint)point withEvent:(UIEvent *)event hitTest:(CGPoint)point withEvent:(UIEvent *)event

为什么呢?感谢。

1 个答案:

答案 0 :(得分:0)

I got a clue from Why does UINavigationBar steal touch events?

I guess navigation bar steal touch event and dispatch the touch event to textfield directly.

So Here is the solution. It works well.

@implementation UINavigationBar(Xxxxxx)

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *v = [super hitTest:point withEvent:event];
    return v == self ? nil: v;
}

@end