键盘代理根据虚拟键盘弹出和收回控制UITextfield位置

最近个人几篇文章介绍了改键盘代理的文章. 关联文章的地址

    因为iOS移动设备屏幕大小有限,不能像桌面用鼠标随便拖动,所以相似登岸页面输入时,常把输入框避开虚拟键盘,或者虚拟键盘弹出时输入框移动到可见位置。需要做的是让UItextfield设置代理,然后利用其代理方法:

    

    //键盘收回时代理函数,这里设置登岸框归位

    - (void)textFieldDidEndEditing:(UITextField *)textField{//

    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationCurveLinear animations:^{

    [loginView setFrame:CGRectMake(296, 376, 432, 300)];

    } completion:nil];

    }

    //键盘弹出时代理函数,这里设置登岸框移动到可见区域

    - (void)textFieldDidBeginEditing:(UITextField *)textField{

    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationCurveLinear animations:^{

    [loginView setFrame:CGRectMake(296, 88, 432, 300)];

    } completion:nil];

    }

 

    //点击肯定后键盘收回

    -(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];

    return YES;

    }

    

    

 此外,中文输入法不同于英文输入法会有选字的区域,也就是说比英文输入法键盘高

    

键盘代理根据虚拟键盘弹出和收回控制UITextfield位置_第1张图片

    

 

    

高度不一样

    

 

    

键盘代理根据虚拟键盘弹出和收回控制UITextfield位置_第2张图片

    

所以要想真正让输入框适应键盘高度,应当获取键盘的动态高度然后让输入框位置匹配,添加一个监听通知然后修改位置:
    每日一道理
如果说生命是一座庄严的城堡,如果说生命是一株苍茂的大树,如果说生命是一只飞翔的海鸟。那么,信念就是那穹顶的梁柱,就是那深扎的树根,就是那扇动的翅膀。没有信念,生命的动力便荡然无存;没有信念,生命的美丽便杳然西去。(划线处可以换其他词语)

    

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

- (void)keyboardWillShow:(NSNotification *)notification

{

NSDictionary *info = [notification userInfo];

CGSize kbSize = [[info objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size;

//UIKeyboardCenterEndUserInfoKey

int changeBalue=715-kbSize.height;

[searchView setFrame:CGRectMake(0, changeBalue, 284, 53)];

}

但是实际上objectForKey:UIKeyboardBoundsUserInfoKey已经被弃用了,虽然现在还可以用,文档说提议用UIKeyboardFrameBeginUserInfoKey替换,使用方法以后更新









-(void)textFieldDidBeginEditing:(UITextField *)textField

{

    

    userName.hidden=YES;

    loginPass.hidden=YES;

    loginButton.hidden=YES;

    [selfregNotification];

}

-(void)textFieldDidEndEditing:(UITextField *)textField

{


    userName.hidden=NO;

    loginPass.hidden=NO;

    loginButton.hidden=NO;

    [selfunregNotification];


}


- (void)regNotification

{

   [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotificationobject:nil];


}


- (void)unregNotification

{

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardWillChangeFrameNotificationobject:nil];

}


#pragma mark - notification handler


- (void)keyboardWillChangeFrame:(NSNotification *)notification

{

    NSDictionary *info = [notification userInfo];

    CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];

    CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;

    

    CGRect inputFieldRect = RegName.frame;

    CGRect moreBtnRect = RegPass.frame;

    

    inputFieldRect.origin.y += yOffset+50;

    moreBtnRect.origin.y += yOffset+50;

    

    NSLog(@"%f-----%f--------%f----%f",inputFieldRect.origin.x,inputFieldRect.origin.y,inputFieldRect.size.width,inputFieldRect.size.height);

    

    [UIViewanimateWithDuration:duration animations:^{

        RegName.frame = inputFieldRect;

        RegPass.frame = moreBtnRect;

    }];

}


文章结束给大家分享下程序员的一些笑话语录: 腾讯总舵主马化腾,有人曾经戏称如果在Z国选举总统,马化腾一定当选,因为只要QQ来一个弹窗”投马总,送Q币”即可。

你可能感兴趣的:(UITextField)