iOS 多个UITextField切换,键盘遮挡输入框问题

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "ViewController.h"
#import "KKTextField.h"
@interface ViewController ()<UITextFieldDelegate>
@end

@implementation ViewController
- (void)viewDidLoad 
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yjd_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
for (int i = 0; i < 4; i++)
    {
        UITextField *textfield = [[UITextField alloc] init];
        textfield.frame = CGRectMake(50, 300 + i * 50, 200, 40);
        textfield.layer.cornerRadius = 5;
        textfield.layer.borderColor = [UIColor redColor].CGColor;
        textfield.layer.borderWidth = 1;
        textfield.backgroundColor = [UIColor lightGrayColor];
        textfield.tag = 200 + i;
        textfield.delegate = self;
        textfield.returnKeyType = i==3?UIReturnKeyDefault:UIReturnKeyNext;
        [self.view addSubview:textfield];
    }
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEdit];
}
- (void)endEdit
{
    [self.view endEditing:YES];
    self.view.transform = CGAffineTransformIdentity;
}
- (void)yjd_keyboardWillShow:(NSNotification *)notifi
{
    //获取键盘的高度
    NSDictionary *userInfo = [notifi userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGFloat keyboardHeight = [aValue CGRectValue].size.height;//键盘的高度
    //获取键盘动画时间
    CGFloat time = [notifi.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    //获取当前第一响应状态的输入框
    UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [keyWindow performSelector:@selector(firstResponder)];
    
    //输入框在当前屏幕的坐标y
    CGFloat maxY =  CGRectGetMaxY([view convertRect:view.bounds toView:[[[UIApplication sharedApplication] delegate] window]]);
    
    //判断是非遮挡当前输入框,小于0遮挡,大于或等于0没有
    CGFloat map = SCREEN_HEIGHT - maxY - keyboardHeight ;
    NSLog(@"map = %f",map);
    if (map < 0)
    {
        [UIView animateWithDuration:time animations:^{
            self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, map);
        }];
    }
}
#pragma mark UITextFieldDelegate 事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType == UIReturnKeyNext)
    {
        UITextField *nextTextField = (UITextField *)[self.view viewWithTag:textField.tag+1];
        [textField endEditing:YES];
        [nextTextField becomeFirstResponder];
    }
    else
    {
        [self endEdit];
    }
    return YES;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,155评论 3 119
  • 我是一个健忘的人。 对于一个健忘的人,过去与未来都是苍茫一色。 “天之苍苍其正色邪,其远而无所至极邪?” 朋友中有...
    何青猊阅读 298评论 0 1
  • 在古代,我名字里的暻等同于景,歌就是歌曲的意思吧。 父母在起名时可能看过了万千景色,所以我的名字叫“暻歌”。 ...
    zzZxxX_4111阅读 373评论 0 0
  • 壹 晓丽战友成立了一个2018瘦身部落,期间,大家了解了各种科学瘦身的方法,每天进行一定量的有氧运动,保持饮食结构...
    Kim生活阅读 213评论 0 0
  • 吃住是旅游的最基本要素。吃没有吃过的,住没有住过的,吃还要讲究特色,营养,住还要讲究奢华舒适。 4月中旬到5月...
    朝华景意阅读 330评论 0 0

友情链接更多精彩内容