scrollview,键盘遮挡输入框,上移
2017-07-21 13:31
239 查看
// // ViewController.m // 键盘上移 // // Created by lcy on 16/7/21. // Copyright © 16年 NJ. All rights reserved. // #import "ViewController.h" @interface ViewController ()<UITextFieldDelegate> @property(nonatomic,assign)CGFloat keyboardHeight;//键盘高度 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _toptextF.delegate = self; _middleTextF.delegate = self; _bottomTextF.delegate = self; // 手势回收键盘 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardDown)]; [self.view addGestureRecognizer:tap]; // 监听键盘弹出,获取键盘的高度 NSNotificationCenter * defaultCenter = [NSNotificationCenter defaultCenter]; [defaultCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } //UITextFieldDelegate -(void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame = textField.frame; int screenH = [UIScreen mainScreen].bounds.size.height; // 当前点击textfield的坐标的Y值 + 当前点击textFiled的高度 -(屏幕高度- 键盘高度 - 键盘上tabbar高度/状态栏的高度) // ✅textfile的的最大Y值 和 键盘的最全高度的差值,用来计算整个view的偏移量 int offset = frame.origin.y + CGRectGetHeight(textField.frame) -( screenH - _keyboardHeight - 20); NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyBoard" context:nil]; [UIView setAnimationDuration:animationDuration]; if(offset > 0) // >0,说明键盘挡住 { [UIView animateWithDuration:0.4 animations:^{ self.backScrollView.contentOffset = CGPointMake(0, offset); }]; } } //输入框编辑完成以后,将视图恢复到原始状态 -(void)textFieldDidEndEditing:(UITextField *)textField { NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; [UIView animateWithDuration:0.4 animations:^{ self.backScrollView.contentOffset = CGPointMake(0, 0); }]; [UIView commitAnimations]; } -(void)keyboardDown{ [self.view endEditing:YES]; } -(void)keyboardWillShow:(NSNotification *)aNotification{ //获取键盘的高度 NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; _keyboardHeight = keyboardRect.size.height; } @end
[b]①以下几种键盘类型几乎一样,键盘高度也是一样的
[/b]
UIKeyboardTypeAlphabet
UIKeyboardTypeASCIICapable
UIKeyboardTypeDefault
UIKeyboardTypeEmailAddress
UIKeyboardTypeNamePhonePad
UIKeyboardTypeNumbersAndPunctuation(数字和标点符号)
UIKeyboardTypeTwitter
UIKeyboardTypeURL
UIKeyboardTypeWebSearch
5.5吋271
4.7吋258
4.0吋253
②以下几种键盘为数字类型的键盘,键盘高度也是一样的
UIKeyboardTypeDecimalPad(带小数点的数字键盘)
UIKeyboardTypeNumberPad(纯数字键盘)
UIKeyboardTypePhonePad(带*+#,;的数字键盘)
5.5吋226
4.7吋216
4.0吋216
相关文章推荐
- 输入框被键盘遮挡时 让整个view上移
- [Cordova/Phonegap] Cordova iOS 应用在第三方输入法的键盘弹出(点击输入框)时,页面不上移,导致输入框被键盘遮挡 的解决办法
- IOS 键盘遮挡输入框 视图上移解决方案
- iOS键盘遮挡输入框,输入区域自动上移
- android,键盘遮挡了输入框,完美解决android键盘遮挡问题
- iOS项目开发键盘弹出遮挡输入框问题解决方案
- android输入框被键盘遮挡
- ios键盘不遮挡输入框
- 输入框被键盘遮挡问题
- android软键盘遮挡输入框问题
- React Native 解决ScrollView 上 TextInput 键盘遮挡问题
- 解决键盘遮挡输入框(UITextField)问题
- iOS TextField 弹出键盘时实现view整体上移下移:防止弹出键盘遮挡输入框
- iOS讲解迷惑之键盘弹出遮挡输入框的问题
- 在ios中避免键盘遮挡了输入框的处理
- React-native键盘遮挡输入框问题的解决
- iOS键盘弹出遮挡输入框问题
- UITableView键盘遮挡 自动上移
- Android键盘遮挡输入框的问题
- 输入框被键盘遮挡时 让整个view上移