一、关于textField键盘回收
先签textField的协议,写个属性
@interface ResumeViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *majorTF;//所学专业
@property(nonatomic,strong)UITextField *heightTF;//身高
@property(nonatomic,strong)UITextView *hobbyTextView;//特长与爱好
@end
创建textField代码如下:
self.majorTF = [[UITextField alloc]init];
[majorView addSubview:self.majorTF];
self.majorTF.delegate = self;
self.majorTF.tag = 3000;
self.majorTF.backgroundColor = [UIColor lightGrayColor];
[self.majorTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(majorL.mas_right).offset(8);
make.centerY.equalTo(majorView.mas_centerY);
make.height.mas_offset(35);
make.width.mas_offset(WIDTH-150*ScreenWidth/375);
}];
#pragma mark -------- textField键盘回收 -------------
//将要开始输入时 让屏幕上移 露出输入框
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.tag == 1000) {
[UIView animateWithDuration:0.1 animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 258);
}];
}else if (textField.tag == 3000){
[UIView animateWithDuration:0.1 animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - (258+35));
}];
}
return YES;
}
//将要结束时 让屏幕下移
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if (textField.tag == 1000) {
[UIView animateWithDuration:0.001 animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 258);
}];
}else if (textField.tag == 3000){
[UIView animateWithDuration:0.001 animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + (258+35));
}];
}
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
二、关于textField键盘限制只允许输入指定位数内容
self.heightTF = [[UITextField alloc]init];
[heightView addSubview:self.heightTF];
self.heightTF.keyboardType = UIKeyboardTypeNumberPad;
self.heightTF.backgroundColor = [UIColor whiteColor];
self.heightTF.delegate = self;
[self.heightTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(heightL.mas_right).offset(8);
make.centerY.equalTo(heightView.mas_centerY);
make.height.mas_offset(35);
make.width.mas_offset(36);
}];
#pragma mark ------ textField 编辑输入内容 --------
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{ //string就是此时输入的那个字符textField就是此时正在输入的那个输入框返回YES就是可以改变输入框的值NO相反
if ([string isEqualToString:@"\n"]) //按会车可以改变
{
return YES;
}
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
if (self.heightTF == textField) //判断是否时我们想要限定的那个输入框
{
if ([toBeString length] > 3) { //如果输入框内容大于20则弹出警告
textField.text = [toBeString substringToIndex:3];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"超过最大字数不能输入了" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
return NO;
}
}
return YES;
}
三、关于textView键盘回收
self.hobbyTextView = [[UITextView alloc]init];
self.hobbyTextView.backgroundColor = [UIColor lightGrayColor];
[hobbyView addSubview:self.hobbyTextView];
self.hobbyTextView.delegate = self;
self.hobbyTextView.tag = 2000;
self.hobbyTextView.font = [UIFont systemFontOfSize:18];
[self.hobbyTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(hobbyL.mas_right).offset(8);
make.centerY.equalTo(hobbyView.mas_centerY);
make.height.mas_offset(80);
make.width.mas_offset(WIDTH-150*ScreenWidth/375);
}];
#pragma mark ------------ textView 键盘回收 -----------------
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if (textView.tag == 2000) {
[UIView animateWithDuration:0.1 animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y - (258+35));
}];
}
return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
if (textView.tag == 2000) {
[UIView animateWithDuration:0.001 animations:^{
self.view.center = CGPointMake(self.view.center.x, self.view.center.y + (258+35));
}];
}
return YES;
}
我这里,对键盘回收时创建一个按钮,方便用户点击将键盘回收
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 35)];
[topView setBarStyle:UIBarStyleDefault];
UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 35)];
[topView addSubview:downView];
UITapGestureRecognizer *tapAction = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappBtnAction)];
[downView addGestureRecognizer:tapAction];
UIImageView *downImage = [[UIImageView alloc]init];
[downView addSubview:downImage];
[downImage mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(topView.mas_centerX);
make.centerY.equalTo(topView.mas_centerY);
make.width.mas_offset(30);
make.height.mas_offset(35);
}];
downImage.image = [UIImage imageNamed:@"向下箭头"];
[_nameTF setInputAccessoryView:topView];
[_nationTF setInputAccessoryView:topView];
[_heightTF setInputAccessoryView:topView];
[_graduationTF setInputAccessoryView:topView];
[_schoolTF setInputAccessoryView:topView];
[_majorTF setInputAccessoryView:topView];
[_localTF setInputAccessoryView:topView];
[_hobbyTextView setInputAccessoryView:topView];
}
-(void)tappBtnAction{
[_nameTF resignFirstResponder];
[_nationTF resignFirstResponder];
[_heightTF resignFirstResponder];
[_graduationTF resignFirstResponder];
[_schoolTF resignFirstResponder];
[_majorTF resignFirstResponder];
[_localTF resignFirstResponder];
[_hobbyTextView resignFirstResponder];
}