iOS开发中,注册登录页面可以说是必不可少的,我们经常遇到的问题无非就是键盘弹出之后遮住了输入框。所以我们要在键盘弹出或收回的同时调节输入框的高度,进而解决键盘遮住输入框的问题!!!
先来个懒人版的,直接把第三方库丢进工程,啥都不用管了,自动帮你调节,地址在这里:IQKeyboardManager,赶紧下载试试吧!
尽管IQKeyboardManager已经很强大了,但是在实际开发过程中,有些地方还是不尽人意,所以要自己搞调整,下面是手动处理的,根据需求,定制最适合自己的方法。
.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITextFieldDelegate>
@end
.m文件
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//单行文本框
UITextField *userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(38, 197, 249, 30)];
[self.view addSubview:userNameTextField];
//1.设置代理
userNameTextField.delegate = self;
//2.文本框外观效果
//边框样式
userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
//占位符
userNameTextField.placeholder = @"用户名";
//文本框的左右控件
//默认自带的清除按钮
//userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
//自定义左右视图
UIButton *myClearButton = [UIButton buttonWithType:UIButtonTypeSystem];
myClearButton.frame = CGRectMake(0, 0, 16, 16);
myClearButton.backgroundColor = [UIColor redColor];
[myClearButton setTitle:@"×" forState:UIControlStateNormal];
[myClearButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
userNameTextField.keyboardType = UIKeyboardTypeASCIICapable;
//放置右视图,设置模式
userNameTextField.rightView = myClearButton;
userNameTextField.rightViewMode = UITextFieldViewModeWhileEditing;
#warning 为清除按钮添加清除用户名文本框的行为:
//另外还有leftView,请自行尝试
//3.再创建一个密码文本框,设置效果
UITextField *passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(38, 252, 249, 30)];
[self.view addSubview:passwordTextField];
passwordTextField.delegate = self;
passwordTextField.borderStyle = UITextBorderStyleRoundedRect;
passwordTextField.placeholder = @"密码";
UILabel *passwordStrengthLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 16, 16)];//显示密码“强中弱”的标签
passwordStrengthLabel.font = [UIFont systemFontOfSize:12];
passwordTextField.rightView = passwordStrengthLabel;
passwordTextField.rightViewMode = UITextFieldViewModeAlways;
passwordTextField.secureTextEntry = YES;
//添加响应行为使得文本框输入文本后作检测
[passwordTextField addTarget:self action:@selector(passwordTextFieldTextChanged:) forControlEvents:UIControlEventEditingChanged];
//4.键盘通知
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillShowNotification object:nil];
[defaultCenter addObserver:self selector:@selector(keyboardWillShowOrHide:) name:UIKeyboardWillHideNotification object:nil];
//5.自定义键盘附件和自定义键盘
//例如,给没有return键的键盘加上自定义的return键
passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
UIButton *returnButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 1, 50)];
returnButton.backgroundColor = [UIColor lightGrayColor];
[returnButton setTitle:@"⬇️" forState:UIControlStateNormal];
//点击按钮时直接让文本框执行取消第一响应者的方法
[returnButton addTarget:passwordTextField action:@selector(resignFirstResponder) forControlEvents:UIControlEventTouchUpInside];
passwordTextField.inputAccessoryView = returnButton;//键盘附件
UIView *myKeyboard = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 100)];
myKeyboard.backgroundColor = [UIColor yellowColor];
//passwordTextField.inputView = myKeyboard;//自定义键盘
}
#pragma mark - 键盘通知回调
- (void)keyboardWillShowOrHide:(NSNotification *)notification
{
//获取通知名
NSString *notificationName = notification.name;
//获取通知内容
NSDictionary *keyboardInfo = notification.userInfo;
//键盘弹出时,让画面整体稍稍上移,并伴随动画
//键盘回收时反之
NSLog(@"keyboardInfo: %@", keyboardInfo);
//动画结束后self.view的frame值
CGRect selfViewFrame = self.view.frame;
//通过通知名字判断弹出还是回收
if ([notificationName isEqualToString:UIKeyboardWillShowNotification]) {
selfViewFrame.origin.y = -50;
}
else {
selfViewFrame.origin.y = 0;
}
//取出动画时长
NSTimeInterval duration = [keyboardInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//使用动画更改self.view.frame
[UIView animateWithDuration:duration animations:^{
//这里填入一些view的最终状态属性设置,即会自动产生过渡动画
self.view.frame = selfViewFrame;
}];
}
#pragma mark - 密码文本框发生改变后
- (void)passwordTextFieldTextChanged:(UITextField *)textField
{
NSLog(@"密码发生了改变");
//假设按照密码的长度区分强(>9)、中(6~9)、弱(<6)
NSUInteger passwordLength = textField.text.length;
NSString *strengthText = nil;//描述强度的文本
UIColor *strengthColor = nil;//描述强度的颜色
if (passwordLength < 6) {
strengthText = @"弱";
strengthColor = [UIColor redColor];
}
else if (passwordLength <= 9) {
strengthText = @"中";
strengthColor = [UIColor orangeColor];
}
else {
strengthText = @"强";
strengthColor = [UIColor greenColor];
}
//取出rightView并设置属性
UILabel *rightView = (UILabel *)textField.rightView;
rightView.text = strengthText;
rightView.textColor = strengthColor;
}
#pragma mark - 文本框代理方法⬇️
#pragma mark - 文本框文字发生改变之前调用
//判断文本将会如何改变,返回一个布尔值表示是否允许改变
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//获得改变前(现在)的文本
NSString *currentText = textField.text;
//“预测”改变后的文本
NSString *changedText = [currentText stringByReplacingCharactersInRange:range withString:string];
//判断“未来”的文本长度是否大于10
if (changedText.length > 10) {
//不允许更改
return NO;
}
else {
//允许更改
return YES;
}
}
#pragma mark - 点击键盘return键时
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//我选择在这个地方回收键盘
//当文本框处于编辑状态时就是第一响应者,想回收键盘,就要取消第一响应者
[textField resignFirstResponder];
return NO;
}
@end
效果图: