Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TextField动画破坏ios8_Ios_Xcode_Uitextfield_Uiviewanimation - Fatal编程技术网

TextField动画破坏ios8

TextField动画破坏ios8,ios,xcode,uitextfield,uiviewanimation,Ios,Xcode,Uitextfield,Uiviewanimation,这段代码在ios 7中运行得很好。然而,在ios8和xcode 6.0.1中,它已经停止工作。当用户单击文本字段以输入文本时,该字段将被设置为浮动在键盘顶部的正上方,以便他们可以看到正在键入的内容。你有没有想过为什么现在这样做行不通。我可以看到它在瞬间开始动画化,但随后文本字段消失了。令人沮丧 - (void)textFieldDidBeginEditing:(UITextField *)textField { [UIView beginAnimations:nil context:NULL];

这段代码在ios 7中运行得很好。然而,在ios8和xcode 6.0.1中,它已经停止工作。当用户单击文本字段以输入文本时,该字段将被设置为浮动在键盘顶部的正上方,以便他们可以看到正在键入的内容。你有没有想过为什么现在这样做行不通。我可以看到它在瞬间开始动画化,但随后文本字段消失了。令人沮丧

- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.4];
[UIView setAnimationBeginsFromCurrentState:YES];
textField.frame = CGRectMake(textField.frame.origin.x, (textField.frame.origin.y - 200.0),            
textField.frame.size.width, textField.frame.size.height);

_searchBtn.frame = CGRectMake(_searchBtn.frame.origin.x, (_searchBtn.frame.origin.y - 200.0), 
_searchBtn.frame.size.width, _searchBtn.frame.size.height);
[UIView commitAnimations];


textField.alpha = .75;


}

问题是代码将文本字段的位置调整为200.0的固定值

这对于iOS7来说可能很好,但在iOS8中情况发生了变化,原因有两个:

  • 系统键盘还有一个额外的视图,用于在键入时显示预测的单词
  • 自定义键盘的高度可以由开发人员选择
无论何时显示或隐藏键盘,您都需要使用以下两种通知更改方法移动文本字段的位置:

  • UIKeyboardWillShowNotification
  • UIKeyboardWillHideNotification
在以下线程中,我将解释可能出现的问题以及如何在键盘打开时正确移动视图:

编辑1: 假设textbox有一个名为“yourTextBox”的出口,修改textbox位置的代码可能如下所示:

CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
CGFloat keyboardHeight = <<calculated keyboard height as previously discussed>>;
CGFloat textBoxHeight = _yourTextBox.frame.size.height;

// Change the current frame of your textbox in order to reposition it
CGFrame textBoxFrame = _yourTextBox.frame;
textBoxFrame.origin.y = screenHeight - keyboardHeight - textBoxHeight;
_yourTextBox.frame = textBoxFrame;
CGFloat屏幕高度=[[UIScreen mainScreen]界限].size.height;
CGFloat键盘高度=;
CGFloat textBoxHeight=\u yourTextBox.frame.size.height;
//更改文本框的当前帧以重新定位它
CGFrame textBoxFrame=\u yourTextBox.frame;
textBoxFrame.origin.y=屏幕高度-键盘高度-textBoxHeight;
_yourTextBox.frame=textBoxFrame;

注意:如果使用自动布局约束来定位子视图,则需要避免修改框架,而是更改约束。如果你在这方面有问题,就发帖子,因为这可能会变得棘手。

看起来你和下面的人有同样的问题。也许他的问题能给你一些答案。如果他的问题包括你的,你应该投上一票以引起更多的注意。但我猜他的问题会对你有所帮助。例如,您的200是硬编码的。但是你应该注意键盘上的提示,这样你就可以提取高度了。为了测试,试着把数字改为250,看看会发生什么。谢谢你的回复,康索尔。我尝试根据键盘的实际高度动态调整代码,但仍然没有成功。我还试着玩这些数字。非常感谢您迄今为止提出的建议。必须有人对此有答案!太好了,谢谢!我已经看完了你的帖子,正在落实你的建议。但是,如果将文本框移动到当前键盘高度之上,我的代码会是什么样子?它仍然是我们的品牌吗?