// // MyUITextFieldViewController.m // 2020082901Test // // Created by 张亚楠 on 2021/1/25. // Copyright © 2021 张亚楠. All rights reserved. // #import "MyUITextFieldViewController.h" #import <Masonry.h> @interface MyUITextFieldViewController ()<UITextFieldDelegate> @property (nonatomic, strong, readwrite) UITextField *textField; @property (nonatomic, strong, readwrite) UITextField *centerTextField; @property (nonatomic, strong, readwrite) UITextField *bottomTextField; @property (nonatomic, copy) NSMutableArray *textFieldArray; @end @implementation MyUITextFieldViewController - (NSMutableArray *)textFieldArray { if (_textFieldArray == nil) { NSMutableArray *mArray = [NSMutableArray array]; [mArray addObject:self.textField]; [mArray addObject:self.centerTextField]; [mArray addObject:self.bottomTextField]; _textFieldArray = mArray; } return _textFieldArray; } - (void)viewDidLoad { [super viewDidLoad]; self.textField = [[UITextField alloc]init]; self.centerTextField = [[UITextField alloc]init]; self.bottomTextField = [[UITextField alloc]init]; [self.view addSubview:self.textField]; [self.view addSubview:self.centerTextField]; [self.view addSubview:self.bottomTextField]; [self.textField mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(120); make.left.mas_equalTo(20); make.right.mas_equalTo(-20); make.height.mas_equalTo(40); }]; [self.centerTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(400); make.left.mas_equalTo(20); make.right.mas_equalTo(-20); make.height.mas_equalTo(40); }]; [self.bottomTextField mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(600); make.left.mas_equalTo(20); make.right.mas_equalTo(-20); make.height.mas_equalTo(40); }]; [self initTextField:self.textField leftImage: [UIImage imageNamed:@"蓝色我的图标"]]; [self initTextField:self.centerTextField leftImage: [UIImage imageNamed:@"蓝色我的图标"]]; [self initTextField:self.bottomTextField leftImage: [UIImage imageNamed:@"蓝色我的图标"]]; //设置returnKeyType,可以直接跳转到下一个填空 for (int i = 0; i < self.textFieldArray.count; i++) { UITextField *textField = self.textFieldArray[i]; textField.delegate = self; textField.tag = i; textField.returnKeyType = i == (self.textFieldArray.count - 1) ? UIReturnKeyDefault : UIReturnKeyNext; } //添加键盘监听事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yjd_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; } - (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 = [UIScreen mainScreen].bounds.size.height - maxY - keyboardHeight; NSLog(@"map = %f", map); if (map < 0) { [UIView animateWithDuration:time animations:^{ self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, map); }]; } } //点击键盘return - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField.returnKeyType == UIReturnKeyNext) { NSLog(@"textFieldShouldReturn"); UITextField *nextTextField = (UITextField *)[self.view viewWithTag:textField.tag + 1]; [textField endEditing:YES]; [nextTextField becomeFirstResponder]; } else { NSLog(@"textFieldShouldReturn===transform"); [self endEdit]; } return YES; } //隐藏键盘 - (void)endEdit { [self.view endEditing:YES]; self.view.transform = CGAffineTransformIdentity; } //点击其他位置隐藏键盘 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self endEdit]; } - (void)dealloc { //移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; } //初始化UITextField - (void)initTextField:(UITextField *)textField leftImage:(UIImage *)leftImage{ //设置边框 textField.layer.borderColor = [UIColor grayColor].CGColor; textField.layer.borderWidth = 1.0; textField.layer.cornerRadius = 10; //设置内边距、paddingTop、paddingLeft、paddingBottom、paddingRight // [textField setValue:[NSNumber numberWithInt:20] forKey:@"paddingLeft"]; UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; //设置左侧“返回” UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 25, 24)]; imageView.image =leftImage; [leftView addSubview:imageView]; textField.leftView = leftView; //设置显示模式 textField.leftViewMode = UITextFieldViewModeAlways; textField.clearButtonMode = UITextFieldViewModeWhileEditing; //设置代理 textField.delegate = self; } @end