gpt4 book ai didi

iPhone - 键盘隐藏文本字段

转载 作者:行者123 更新时间:2023-12-03 18:18:37 25 4
gpt4 key购买 nike

我正在使用 UITextField 来接收用户输入。但是,由于我的文本字段位于 Nib 的中间/底部,因此当键盘弹出时它会被隐藏。有什么方法可以将其与键盘一起滑动,以便在选择过程中它位于键盘的顶部吗?另外,由于我也使用数字键盘,是否有一种简单的方法可以在某处包含完成按钮?

感谢您的帮助。

最佳答案

我为这个问题做了一个简单的应用程序。它会自动检查文本字段的位置,如果键盘隐藏它,它将根据需要自动向上移动。


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}


- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int animatedDistance;
int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{

animatedDistance = 216-(460-moveUpValue-5);
}
else
{
animatedDistance = 162-(320-moveUpValue-5);
}

if(animatedDistance>0)
{
const int movementDistance = animatedDistance;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
}

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

[textField resignFirstResponder];
return YES;
}

关于iPhone - 键盘隐藏文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2307200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com