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);
    }];
}
相关推荐
2601_958320571 天前
【详细版教程】Windows/macOS/Linux 安装 OpenClaw 2.6.6 指南(包含安装包)
linux·运维·windows·macos·小龙虾·open claw一键安装
HackTwoHub1 天前
可视化未授权访问批量探测工具、支持批量目标、并发扫描、SOCKS5 全局代理、CSV 导出
linux·windows·macos·网络安全·网络攻击模型
jerrywus1 天前
别再陪 AI 调 iOS 了:用 cmux + baguette,让 Claude 在你的模拟器里"自己动手"
前端·ios·claude
MonkeyKing71551 天前
iOS 开发 Block 底层结构、循环引用及解决方案
ios·面试
文件夹__iOS1 天前
Swift 5.9 被严重低估的特性:参数包,一次性干掉重复泛型重载
ios·swiftui·swift
薛定猫AI1 天前
【技术干货】用 AI + Expo 打通 iOS / Android / Web 跨端应用开发:从架构到代码生成实战
android·人工智能·ios
MonkeyKing1 天前
iOS关联对象底层实现与内存管理细节
ios
Allen Su1 天前
【Mac 教程系列第 19 篇】如何将 macOS 中的 Microsoft AutoUpdate 软件从系统中删除
microsoft·macos·autoupdate
丝雨_xrc1 天前
Minimax M2.7 多模态应用落地实战指南
ide·macos·xcode
90后的晨仔2 天前
SwiftUI 高级特性第2章:组合与容器
ios