标签:style blog io color os ar for sp div
在iOS开发的时候有两个经常要用到的控件UITextfield跟UITextView,我们输入内容基本是通过这两个控件进行的,但是有时候会遇到这样的问题:在点击输入之后弹出键盘遮盖住了输入框,可以通过以下办法解决:
添加通知监听键盘的弹出跟隐藏
//监听键盘弹出和隐藏 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
在键盘弹出或隐藏的时候进行以下操作:
- (void)keyboardWillShow:(NSNotification *)note { //取得键盘弹出时间 CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; //取得键盘高度 CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardHeight = keyboardFrame.size.height; //option的值设置为7 << 16会让view跟键盘弹出效果同步 [UIView animateWithDuration:duration delay:0 options:7 << 16 animations:^{ self.ContentSv.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight); } completion:nil]; } - (void)keyboardWillHide:(NSNotification *)note { CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue]; [UIView animateWithDuration:duration delay:0 options:7 << 16 animations:^{ self.ContentSv.transform = CGAffineTransformIdentity; } completion:nil]; }
标签:style blog io color os ar for sp div
原文地址:http://www.cnblogs.com/shaohuaios/p/4059588.html