gpt4 book ai didi

iphone - 在输入过程中移动文本字段

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:13 24 4
gpt4 key购买 nike

我设置了一些文本字段和标签,使其看起来像一个需要用户输入的简单表单。然而,我的问题是,只要选择了一些文本字段并且键盘滑出,文本字段就会被覆盖,因此无法看到输入。我的问题是如何向上移动文本字段,以便它们在被选中时保持在 View 中。也许更有经验的人可以帮助我解决这个问题。

最佳答案

我喜欢做的一个很好的解决方案是在文本字段开始使用以下委托(delegate)函数进行编辑时收听:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//keep a member variable to store where the textField started
_yPositionStore = textField.frame.origin.y;

//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//
//I am adding an animation to make this look better
[UIView beginAnimations:@"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];

commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
160 , //this is just a number to put it above the keyboard
commentTextField.frame.size.width,
commentTextField.frame.size.height);

[UIView commitAnimations];
}

然后在此回调中,您可以将其返回到其原始位置:

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

[UIView beginAnimations:@"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];

commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
_yPositionStore ,
commentTextField.frame.size.width,
commentTextField.frame.size.height);

[UIView commitAnimations];
}

您需要将 _yPositionStore 声明为 CGFloat 的成员变量,并将您的 textFields 委托(delegate)设置为拥有它的 View Controller (使用界面生成器并将委托(delegate)拖到文件所有者或设置 yourTextField.delegate = self)

希望对你有帮助

关于iphone - 在输入过程中移动文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11462258/

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