网上搜到很多办法,可是发现多多少少都有问题,比如:
1.不支持第三方输入法,比如搜狗
2.有的对系统的不同键盘,支持的也不完善
3.有的是改的键盘工具栏
4.有的把整个工程的都改了
先上段录像吧,这是我的实现效果
。
实现原理:键盘弹出来的时候把scrollow的高度改为屏幕中键盘剩下的高度,计算输入框和scrollow底部距离,这个距离就是scrollow的contentOffset需要改变的。
1.输入框放在一个cell里,实现其代理方法
这个是键盘升起的
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if ([self.superview isKindOfClass:[UIScrollView class]]) {
[GlobalKeyBoardMoveUp scrollView:textField andSuperView:self.superview ];
}
}
这个是收键盘的
//
//收键盘
//
- (void)textFieldDidEndEditing:(UITextField *)textField{
if ([self.superview isKindOfClass:[UIScrollView class]]) {
[UIView animateWithDuration:0.3 animations:^{
[(UIScrollView *)self.superview setContentOffset:CGPointMake(0, 0)];
[(UIScrollView *)self.superview setSize:CGSizeMake(SCREEN_WIDTH_NEW, SCREEN_HEIGHT_NEW)];
} completion:^(BOOL finished) {
}];
}
}
由于系统键盘 第三方键盘无法直接获取高度,因此使用下边方法获取
//
//获取系统键盘高度
//
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextFieldFrame:) name:UIKeyboardDidShowNotification object:nil];
//
//键盘弹起来后才会走这个方法
//
- (void)changeTextFieldFrame:(NSNotification *)noti{
//
//获取系统键盘高度
//
NSDictionary *userInfo = noti.userInfo;
CGRect r = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
_keyBoardHeight = r.size.height;
if([self.rightTextFiled isFirstResponder]){
[self textFieldDidBeginEditing:_rightTextFiled];
}
}
//
//获取系统键盘高度
//
- (double)getKeyBoardHeight{
return _keyBoardHeight;
}
2.键盘上升方法
//
//
//
//
// Created by yfc on 16/9/12.
//
//
#import "GlobalKeyBoardMoveUp.h"
#import "LikeCellView.h"
#import "UIView+Utils.h"
@interface GlobalKeyBoardMoveUp ()
@property(nonatomic,retain)UITextField *textFiled;
@end
@implementation GlobalKeyBoardMoveUp
//
//参数:textFiled输入框 theScrollow需要升降的Scrollow
//
+ (void)scrollView:(UIView *)textFiled andSuperView:(UIScrollView*)theScrollow {
GlobalKeyBoardMoveUp *upAndDown = [[GlobalKeyBoardMoveUp alloc]init];
upAndDown.textFiled = (UITextField*)textFiled;
//
//输入框的坐标转换到滚动view上
//
CGRect textFiledConvertFrame = [textFiled.superview convertRect:textFiled.frame toView:theScrollow];
//
//滚动视图活动的height
//
double heightWithoutKeyBoard = SCREEN_HEIGHT_NEW - [upAndDown differentKeyBoardHeight] - theScrollow.top;
//
//现在键盘已经展示出来了 动画时间要慢于键盘出来时间
//
[UIView animateWithDuration:0.3 animations:^{
theScrollow.height = heightWithoutKeyBoard;
//
//输入框展示不全判断条件。这是和输入框的bottom比的 实际中下方会有空白分割线等 所有减去30 为了能点到下边的输入框减70
//
if(textFiledConvertFrame.origin.y + textFiled.height > (heightWithoutKeyBoard - 70)){
theScrollow.contentOffset = CGPointMake(0, textFiledConvertFrame.origin.y + textFiled.height - (heightWithoutKeyBoard - 70));
}
}];
}
- (double)differentKeyBoardHeight{
//
//新增不同的键盘计算不同的高度
//
double differentKeyBoardHeight = 0;
UIView *inputView_ = _textFiled.inputView;
NSLog(@"键盘高度%f",inputView_.height);
//
//密码键盘
//
if([_textFiled.inputView isKindOfClass:[NSClassFromString(@"iPhoneKeyboard") class]]){
UIView *inputView = _textFiled.inputView;
differentKeyBoardHeight = inputView.height;
if(IsIphoneX){
differentKeyBoardHeight = inputView.height - 49;
}
}
//
//登录键盘
//
else if([_textFiled.inputView isKindOfClass:[NSClassFromString(@"iPhoneKeyboardSpaceNewStyle") class]]){
UIView *inputView = _textFiled.inputView;
differentKeyBoardHeight = inputView.height;
if(IsIphoneX){
differentKeyBoardHeight = inputView.height - 34;
}
}
//
//iPad加密键盘
//
else if ([_textFiled.inputView isKindOfClass:[NSClassFromString(@"iPadKeyboard") class]]){
UIView *inputView = _textFiled.inputView;
differentKeyBoardHeight = inputView.height;
}
//
//系统键盘
//
else{
//
//系统键盘需要通过监听多次才能获取到真实高度
//
if ([_textFiled.superview respondsToSelector:@selector(getKeyBoardHeight)]) {
differentKeyBoardHeight = [((LikeCellView*)_textFiled.superview) getKeyBoardHeight];
if (differentKeyBoardHeight < 1) {
differentKeyBoardHeight = 240 * HEIGHT_SCALE;
if (IS_IPAD) {
differentKeyBoardHeight = 100;
}
}
}else{
differentKeyBoardHeight = 240 * HEIGHT_SCALE;
}
if (IsIphoneX) {
differentKeyBoardHeight -= 34;
}
}
//
//inputAccessoryView也算入键盘高度 _textFiled.inputView 系统键盘是 nil
//
if (_textFiled.inputAccessoryView && _textFiled.inputView != nil) {
differentKeyBoardHeight += _textFiled.inputAccessoryView.height;
}
return differentKeyBoardHeight;
}
@end
#warning 如果模拟器运行 要打开HardWare->Keyboard->Toggle Software Keyboard 而且模拟器上的效果和真机是有差别的
3调用方法
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scrollow = [[UIScrollView alloc]initWithFrame:CGRectZero];
scrollow.top = 0;
scrollow.left = 0;
scrollow.width = SCREEN_WIDTH_NEW;
scrollow.height = SCREEN_HEIGHT_NEW;
scrollow.contentSize = CGSizeMake(SCREEN_WIDTH_NEW, SCREEN_HEIGHT_NEW+1);
[self.view addSubview:scrollow];
#warning 如果模拟器运行 要打开HardWare->Keyboard->Toggle Software Keyboard
double space = 0.0;
//
//这里就只拿系统键盘了 自己定义键盘就不展示了
//
LikeCellView *a1 = [[LikeCellView alloc]initWithFrame:CGRectZero title:@"测试1" placeholder:@"数字键盘" keyBoardType:@"N"];
a1.top = 280;
[scrollow addSubview:a1];
LikeCellView *a2 = [[LikeCellView alloc]initWithFrame:CGRectZero title:@"测试2" placeholder:@"UIKeyboardTypeDefault" keyBoardType:@"UIKeyboardTypeDefault"];
a2.top = a1.bottom + space;
[scrollow addSubview:a2];
LikeCellView *a3 = [[LikeCellView alloc]initWithFrame:CGRectZero title:@"测试3" placeholder:@"UIKeyboardTypeASCIICapable" keyBoardType:@"UIKeyboardTypeASCIICapable"];
a3.top = a2.bottom + space;
[scrollow addSubview:a3];
LikeCellView *a4 = [[LikeCellView alloc]initWithFrame:CGRectZero title:@"测试4" placeholder:@"UIKeyboardTypeNumbersAndPunctuation" keyBoardType:@"UIKeyboardTypeNumbersAndPunctuation"];
a4.top = a3.bottom + space;
[scrollow addSubview:a4];
LikeCellView *a5 = [[LikeCellView alloc]initWithFrame:CGRectZero title:@"测试5" placeholder:@"UIKeyboardTypeURL" keyBoardType:@"UIKeyboardTypeURL"];
a5.top = a4.bottom + space;
[scrollow addSubview:a5];
LikeCellView *a6 = [[LikeCellView alloc]initWithFrame:CGRectZero title:@"测试6" placeholder:@"NN" keyBoardType:@"NN"];
a6.top = a5.bottom + space;
[scrollow addSubview:a6];
scrollow.contentSize = CGSizeMake(SCREEN_WIDTH, a6.bottom +100);
}
代码下载地址 https://github.com/XiaoHeHe1/csdndemo