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);
    }];
}
相关推荐
2501_915909063 小时前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者84 小时前
TraceEagle 代理抓包详解,无感代理、去证书绑定与 HTTP/3 解密能力解析
网络协议·计算机网络·网络安全·ios·adb·https·udp
aiopencode5 小时前
SwiftUI Introspect生产环境完全指南:为什么它是安全可靠的选择
后端·ios
for_ever_love__6 小时前
iOS: 网络管理单例
学习·ios·objective-c
00后程序员张6 小时前
iOS加固技术路线全面解析:Bitcode模式、源码模式与汇编模式对比及爱加密优势
android·汇编·ios·小程序·uni-app·cocoa·iphone
起司喵喵7 小时前
Flutter-MacOS桌面OS系统|flutter.+window_manager客户端OS模板
flutter·macos·策略模式
代码对我眨眼睛7 小时前
macOS Google Chrome 常用快捷键
前端·chrome·macos
啊真真真7 小时前
macOS下libnfc ..写卡失败问题及解决方案
macos·策略模式
叶 落8 小时前
Mac 通过 Miniconda 安装 Python
python·macos·conda·miniconda
ii_best16 小时前
更新!移动端开发软件按键安卓版&手机助手v5.1.0上线!本地AI识别全面解锁,脚本开发再升级
android·人工智能·ios·按键精灵