IOS点击屏幕显隐键盘的几种方法

/**********1.通过通知中心监听键盘显隐***************/

//监听键盘显隐

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWithShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWithHidden:) name:UIKeyboardWillHideNotification object:nil];

#pragma mark 键盘显隐

-(void)keyboardWithShow:(NSNotification *)noti

{

//获取键盘大小 获得键盘坐标

CGSize keyBoardSize = [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

}

-(void)keyboardWithHidden:(NSNotification *)noti

{}

/***********2.使用view的touchesBegan:触摸事件来实现对键盘的隐藏,当点击view的区域就会触发这个事件************/

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

[_searchTextField resignFirstResponder];

}

/***********修改xib中UIView的Custom class为UIControl,UIControl是一些常用控件如UIButton的父类,是UIView的派生类,实现了对触摸和下按的封装。********/

1、首先设置xib中得UIView的Custom class为UIControl

2、设置关系事件,将xib中得UIView拖到.h区中

3、编写隐藏代码:

- (IBAction)touchView:(id)sender {

[self.view endEditing:YES];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容