几种方法隐藏键盘

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{

if ([text isEqualToString:@"\n"]) {

[textview resignFirstResponder];

return NO;

}

return YES;

}

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

[textview resignFirstResponder];

}

-(void)viewWillAppear:(BOOL)animated{

//注册通知,监听键盘弹出事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

//注册通知,监听键盘消失事件

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden) name:UIKeyboardDidHideNotification object:nil];

}

// 键盘弹出时

-(void)keyboardDidShow:(NSNotification *)notification

{

//获取键盘高度

NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect;

[keyboardObject getValue:&keyboardRect];

//调整放置有textView的view的位置

//设置动画

[UIView beginAnimations:nil context:nil];

//定义动画时间

[UIView setAnimationDuration:5];

//设置view的frame,往上平移

[(UIView *)[self.view  viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-50, 320, 50)];

[UIView commitAnimations];

}

//键盘消失时

-(void)keyboardDidHidden

{

//定义动画

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:5];

//设置view的frame,往下平移

[(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-50, 320, 50)];

[UIView commitAnimations];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容