iOS 多个输入框弹出键盘处理

开发中遇到这样一个场景,有多个输入框

而不同的输入框,需要页面向上偏移的距离不一样

这个时候,需要我们在获取到键盘弹出通知的时候,需要

知道我们开始进行编辑的是哪一个输入框,这个时候

需要我们知道一个技术点,就是

**textField的开始编辑的响应方法textFieldDidBeginEditing是比键盘弹出的通知要早的,**根据这个特性,我们就可以在开始编辑的时候,记录一个是哪一个输入框开始了,根据他的位置,设置相应的偏移量

#代码

设置代理

复制代码
    _nameTextField.delegate = self;

实现代理方法

复制代码
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([self.delegate respondsToSelector:@selector(didBeginEditing:)]) {
        [self.delegate didBeginEditingInView:self];
    }
}

实现方法

复制代码
- (void)didBeginEditingInView:(AuthenticationVAccountView *)authenticationAccountView {
    self.editAccountView = authenticationAccountView;
    NSLog(@" didBeginEditingInAuthenticationAccountView");
}

键盘通知

复制代码
- (void)keyboardWillShow:(NSNotification *)noti
{
    AuthenticationVAccountView *userInforView = self.editAccountView;
    
    CGRect userInfoViewframe = [self.scrollView convertRect:userInforView.frame toView:DTContextGet().window];
    CGFloat bottomSpace = UIGetScreenHeight()  - CGRectGetMaxY(userInfoViewframe);
    //设置一个buffer
    bottomSpace -= 40;
    CGRect keyboardRect = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat boardHeight = keyboardRect.size.height;
    if ((boardHeight - bottomSpace) < 0) {
        return;
    }
    CGFloat duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
    Weakify(self);
    [UIView animateWithDuration:duration animations:^{
        Strongify(self);
        self.scrollView.y -= (boardHeight - bottomSpace);
        self.backgroundView.y -= (boardHeight - bottomSpace);
    }];
}
相关推荐
独隅13 小时前
WebStorm macOS 安装调优与实战效能全景
ide·macos·webstorm
2601_9679873715 小时前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
高调的涵涵17 小时前
1GB 文件,df 说没占地方
macos
茶底世界之下1 天前
窗边白雏菊与旧书:用 Awaking 留住一束暖光
ios·app·产品
奶油话梅糖1 天前
苹果的内核跑上了安卓手机:2,631 Star 的 HakiOS 让 XNU 在 Adreno GPU 上画出 118 帧
android·macos·智能手机
独隅2 天前
WebStorm macOS 安装调优与实战效果全景
ide·macos·webstorm
茶底世界之下2 天前
阴天海边的蓝船:用 Awaking 把灰蓝调成清亮主角
ios·app·产品
木子欢儿2 天前
国内 macOS/Linux 安装 rust
linux·运维·服务器·macos·rust
Digitally2 天前
如何将 iPhone 备份到 iCloud?
ios
传奇开心果编程3 天前
【Flutter踩坑排雷】第19课:路由硬编码坑|字符串路由书写错误,页面跳转失败排查
android·flutter·ios·移动开发·跨平台·ui框架·声明式ui