美文网首页
IOS点击屏幕显隐键盘的几种方法

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

作者: plu | 来源:发表于2016-03-30 15:02 被阅读644次

/**********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];

}

相关文章

网友评论

      本文标题:IOS点击屏幕显隐键盘的几种方法

      本文链接:https://www.haomeiwen.com/subject/ifxelttx.html