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

本文介绍如何在iOS应用中调整登录页面布局以适应不同高度的虚拟键盘,包括使用UITextField代理方法来移动输入框,以及通过监听键盘通知来动态调整布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为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;

}

 此外,中文输入法不同于英文输入法会有选字的区域,也就是说比英文输入法键盘高
高度不一样
所以要想真正让输入框适应键盘高度,应该获取键盘的动态高度然后让输入框位置匹配,添加一个监听通知然后修改位置:

[[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;

    [self regNotification];

}

-(void)textFieldDidEndEditing:(UITextField *)textField

{


    userName.hidden=NO;

    loginPass.hidden=NO;

    loginButton.hidden=NO;

    [self unregNotification];


}


- (void)regNotification

{

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];


}


- (void)unregNotification

{

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object: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);

    

    [UIView animateWithDuration:duration animations:^{

        RegName.frame = inputFieldRect;

        RegPass.frame = moreBtnRect;

    }];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值