当用户单击屏幕上的任何区域时,请重新调整键盘

时间:2012-09-05 16:52:16

标签: ios uisearchbar

绘制UISearchBar的框架如下:

UISearchBar *sBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 280, 40)];

当用户点击 ANY 区域而不是上面定义的区域时,我想关闭键盘。

是否有任何UISearchBar委托方法,或者是一种简单的方法。提前完成。

1 个答案:

答案 0 :(得分:1)

您需要在窗口中的其他视图上处理点击/触摸事件,然后致电:

if (sBar.isFirstResponder)
    [sBar resignFirstResponder];

sBar需要是你的UIViewController的属性

要处理触摸事件,请将它们添加到您的UIViewController:

  - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
    }

    - (void) touchesMoved:(NESet *)touches withEvent:(UIEvent *)event
    {
    }

    - (void) touchesEnded:(NESet *)touches withEvent:(UIEvent *)event
    {
    }

- (void) touchesCancelled:(NESet *)touches withEvent:(UIEvent *)event
    {
    }

并至少从touchesBegan方法调用[sBar resignFirstResponder]。