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);
    }];
}
相关推荐
sunfdf3 小时前
移动硬盘上的文件消失了?以下是Mac电脑解决方法
macos·电脑
wonderomg12 小时前
Mac安装openclaw步骤
macos·openclaw
humors22113 小时前
[原创]AI工具:读取手机系统文件工具
windows·ios·安卓·鸿蒙·文件·苹果·读取
whatzhang00713 小时前
在 macOS 上从零配置 Vim:开启语法高亮 + 安装 vim-polyglot + 设置 gruvbox 主题
macos·vim·excel
humors22113 小时前
[原创]AI工具:手机文件查杀病毒工具
windows·ios·手机·安卓·鸿蒙·杀毒·苹果
徐健峰1 天前
Claude Code 安装完全指南(Mac 版):Git、环境变量、PATH 与常见报错一次讲清(2026)
git·macos·arcgis
AppOS1 天前
手把手教你 Openclaw 在 Mac 上本地化部署,保姆级教程!接入飞书打造私人 AI 助手
人工智能·macos·飞书
2501_915918411 天前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
qing222222221 天前
Linux中修改mac地址(重启后依然生效)
linux·服务器·macos
GameTomato1 天前
【Xcode】【资源创建失败】【XCode更新最新版】XCode更新最新版,创建资源文件夹一直读取不到的问题
ide·macos·xcode