iOS 输入时键盘处理问题
2017-11-02 19:49
423 查看
在iOS的开发过程中,有时候需要处理键盘的弹出和收回。
以及键盘弹出收回时、view的处理
最正规的办法,用通知
step 1:
在进入视图的时候添加监视:(viewDidLoad什么的)
step 2:
在键盘动作的时候移动视图:
step 3:
在退出视图的时候注销通知
viewDidUnload:
step 4:
dealloc:
这些代码是摘自apple sample code KeyboardAccessory.
细节自己修改下就好了,比如那个reportTableView、CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 55 - 64)
以及键盘弹出收回时、view的处理
最正规的办法,用通知
step 1:
在进入视图的时候添加监视:(viewDidLoad什么的)
//监听键盘的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
step 2:
在键盘动作的时候移动视图:
- (void)keyboardWillShow:(NSNotification *)notification { /* Reduce the size of the text view so that it's not obscured by the keyboard. Animate the resize so that it's in sync with the appearance of the keyboard. */ NSDictionary *userInfo = [notification userInfo]; // Get the origin of the keyboard when it's displayed. NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position. CGRect keyboardRect = [aValue CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; CGFloat keyboardTop = keyboardRect.origin.y; CGRect newTextViewFrame = self.view.bounds; newTextViewFrame.size.height = keyboardTop - self.view.bounds.origin.y; // Get the duration of the animation. NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; // Animate the resize of the text view's frame in sync with the keyboard's appearance. [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:animationDuration]; self.reportTableView.frame = newTextViewFrame; [UIView commitAnimations]; } - (void)keyboardWillHide:(NSNotification *)notification { NSDictionary* userInfo = [notification userInfo]; /* Restore the size of the text view (fill self's view). Animate the resize so that it's in sync with the disappearance of the keyboard. */ NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; NSTimeInterval animationDuration; [animationDurationValue getValue:&animationDuration]; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:animationDuration]; self.reportTableView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 55 - 64); [UIView commitAnimations]; }
step 3:
在退出视图的时候注销通知
viewDidUnload:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
step 4:
dealloc:
-(void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil]; }
这些代码是摘自apple sample code KeyboardAccessory.
细节自己修改下就好了,比如那个reportTableView、CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height - 55 - 64)
相关文章推荐
- iOS --UITextField 输入值改变事件和键盘遮挡处理
- iOS开发问题----去掉输入键盘上的自动联想部分,限制长度内容,以及禁止输入emoji表情等问题
- ios 处理键盘遮挡问题
- iOS 处理键盘遮挡TextField、TextView问题
- IOS 键盘的显示与关闭,以及移动显示(UITextView处理完整版)(完美中文键盘输入避免触摸无效,做双重保障)
- IOS开发Swift键盘收起,键盘挡住输入模框的处理
- 【iOS】基本控件:文本输入框、开关、分段控件、滑块与输入键盘隐藏的问题
- iOS键盘的监听,以及处理遮挡问题和管理问题
- Ipad/Iphone键盘输入慢、切换数字键盘慢的问题-iOS
- ios tableView多个cell处理键盘遮挡内容视图的问题
- ios上面对输入键盘的处理
- iOS UITextView 限制字数(解决了截断输入联想和对系统键盘中文不友好的问题)
- [iOS备忘录]UITextView多行输入,键盘隐藏问题[swift]
- 修复ios上第三方输入法弹出时输入键盘盖住网页没有进行相应滚动从而盖住表单输入框的问题
- 经典map集合问题,黑马作业题,1.利用键盘录入,输入一个字符串,.统计该字符串中各个字符的数量(提示:字符不用排序)
- iOS键盘输入屏幕上移
- 解决iOS键盘输入中文后视图异常
- 输入的时候被键盘遮挡的问题
- IOS开发-点击View取消键盘输入
- iOS中键盘高度处理