iOS开发之键盘挡住输入框的完美解决办法即输入框位置如何随着键盘的升起而升高,类方法实现,不需要每个输入框写大量代码

 

网上搜到很多办法,可是发现多多少少都有问题,比如:

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

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于在Vue出现的此问题,可以尝试以下解决方案: 1. 使用 `scrollIntoView` 方法:在输入框聚焦时,使用JavaScript的 `scrollIntoView` 方法将页面滚动到输入框位置,确保输入框不被软键盘遮挡。 例如,在输入框的 `@focus` 事件添加以下代码: ```html <input @focus="scrollToInput" /> ... methods: { scrollToInput() { const input = document.getElementById('your-input-id'); input.scrollIntoView(); } } ``` 2. 使用 `window.innerHeight` 监听窗口高度变化:在Vue组件的 `mounted` 生命周期钩子函数,使用 `window.innerHeight` 获取窗口高度,并在窗口高度变化时触发重新计算。 ```html <template> <div ref="container"> <input @focus="adjustInputPosition" /> </div> </template> <script> export default { mounted() { window.addEventListener('resize', this.adjustInputPosition); }, destroyed() { window.removeEventListener('resize', this.adjustInputPosition); }, methods: { adjustInputPosition() { const container = this.$refs.container; const input = container.querySelector('input'); if (input) { const windowHeight = window.innerHeight; const inputBottom = input.getBoundingClientRect().bottom; if (inputBottom > windowHeight) { container.style.transform = `translateY(-${inputBottom - windowHeight}px)`; } else { container.style.transform = 'translateY(0)'; } } } } } </script> ``` 这些解决方案可以确保在聚焦输入框时,软键盘不会遮挡输入框。根据你的具体情况,选择适合你的解决方案即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值