触摸屏幕时如何隐藏键盘(搜索栏) [英] How to hide the keyboard when touching screen (search bar)

查看:209
本文介绍了触摸屏幕时如何隐藏键盘(搜索栏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击搜索或单击取消时键盘隐藏。
但我还想在我点击屏幕上的某个地方时隐藏键盘。
我找到了几个文本教程,但是我们正在使用搜索栏。



有人能告诉我怎么做吗?



谢谢。

解决方案

试试这个



在.h文件中添加UISearchBar

  @property(强,非原子)IBOutlet UISearchBar * searchBar;您的.m档案中

   - (void)viewDidLoad 
{
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)] ;

[self.view addGestureRecognizer:tap];
}


- (void)dismissKeyboard
{
// add self
[self.searchBar resignFirstResponder];
}


The keyboard hides when I click search or when I click on cancel. But I want also that the keyboard hides when I click somewhere on the screen. I found several tutorials for the textfield, but we are using the search bar.

Can someone tell me how to do this?

Thanks.

解决方案

Try This

in your .h file add UISearchBar

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar; 

in your .m file

- (void)viewDidLoad
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];
}


- (void) dismissKeyboard
{
    // add self
    [self.searchBar resignFirstResponder];
}

这篇关于触摸屏幕时如何隐藏键盘(搜索栏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆