iOS 8在出现键盘时向上移动UIView |问题

问题描述:

我有一个UIViewUITextField放置在屏幕的底部,当键盘出现时会向上移动。iOS 8在出现键盘时向上移动UIView |问题

我一直在iOS 8之前的下面的方法,似乎完美的工作。

// When Keyboard appears 
- (void)keyboardWillShow:(NSNotification *)notification { 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey]integerValue]]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

// Frame Update 
CGRect frame = self.bottomView.frame; 
frame.origin.y = self.view.frame.size.height - 266.0f; 
self.bottomView.frame = frame; 

[UIView commitAnimations]; 
} 

// When keyboard disappears 
- (void) keyboardHides : (NSNotification *) notification { 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]]; 
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

// Frame update 
CGRect frame = self.bottomView.frame; 
frame.origin.y = self.view.frame.size.height - self.bottomView.frame.size.height; 
self.bottomView.frame = frame; 

[UIView commitAnimations]; 
} 

但上面的代码似乎并不iOS 8为键盘块背后的UIView的工作。

经过一番研究,我发现了一个almost-similar的答案。但是这里整个UIView被推高了,我想实现的只是移动bottom UIView

+0

为什么不把所有东西都放在'UIScrollView'上,并在出现键盘时向下滚动? – 2014-10-08 13:12:41

+0

我建议你抛弃动画解决方案,并在UITextField上使用'inputAccessoryView'。从我所知道的情况来看,这就是你想要做的。 – Oxcug 2014-10-08 16:50:14

+0

@ Roma-MT不想那么做:) – Sreejith 2014-10-10 02:56:42

获取TPKeyboardAvoidingScrollView从https://github.com/michaeltyson/TPKeyboardAvoiding

使用它,如下所示。

将TPKeyboardAvoidingScrollView.m和TPKeyboardAvoidingScrollView.h源文件拖放到您的项目中,将UIScrollView弹出到视图控制器的xib中,将滚动视图的类设置为TPKeyboardAvoidingScrollView,并将所有控件放入该滚动视图中。您也可以使用编程方式创建它,而不使用xib - 只需使用TPKeyboardAvoidingScrollView作为顶层视图。

要禁用自动“下一步”按钮功能,请将UITextField的返回键类型更改为除UIReturnKeyDefault之外的任何其他键。

+0

这个包的类是惊人的!它运作得很好。谢谢! – Krekin 2015-11-06 07:55:34

+0

确实。完美的作品! – iAnurag 2015-11-06 07:56:52

我不知道,如果这是你的问题,但你应该使用基于块的API来动画一个UIView

例(未测试)

- (void)animateTextField:(UITextField*)textField 
         up:(BOOL)up 
{ 
    const float movementDuration = 0.5f; 
    const int movementDistance = 380; 
    int movement = (up ? -movementDistance : movementDistance); 
    [UIView animateWithDuration:movementDuration 
        animations: 
        ^{ 
         CGRect frame = self.bottomView.frame; 
         frame.origin.y = self.view.frame.size.height - 266.0f; 
         self.bottomView.frame = frame; 

        } 
    ]; 
} 

您可以在苹果的文档阅读:

在iOS 4.0及更高版本中不鼓励使用此方法。您应该使用基于块的动画方法 来指定您的动画。

https://developer.apple.com/Library/ios/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html

希望它可以帮助你!

+0

更改为动画块。 UIView正在更新。但它不反映在用户界面中。添加了[view layoutIfNeeded]。它仍然不起作用。 – Sreejith 2014-10-10 07:16:38

我使用了这段代码。它没有在滚动视图中向上移动。但改变了他们的XY位置。 但这在我的iPhone 6中很有效。我没有用其他iPhone检查它们。

func textViewDidBeginEditing(textView: UITextView){ 
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y - 216 , textView.frame.size.width, textView.frame.size.height) 
    buttonProp.frame = CGRectMake(buttonProp.frame.origin.x, buttonProp.frame.origin.y - 216, buttonProp.frame.size.width, buttonProp.frame.size.height) 
    MessageView.frame = CGRectMake(MessageView.frame.origin.x, MessageView.frame.origin.y - 216, MessageView.frame.size.width, MessageView.frame.size.height) 
    datePicked.frame = CGRectMake(datePicked.frame.origin.x, datePicked.frame.origin.y - 216, datePicked.frame.size.width, datePicked.frame.size.height) 

} 

func textViewDidEndEditing(textView: UITextView){ 
    textView.frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y + 216, textView.frame.size.width, textView.frame.size.height) 
    buttonProp.frame = CGRectMake(buttonProp.frame.origin.x, buttonProp.frame.origin.y + 216, buttonProp.frame.size.width, buttonProp.frame.size.height) 
    MessageView.frame = CGRectMake(MessageView.frame.origin.x, MessageView.frame.origin.y + 216, MessageView.frame.size.width, MessageView.frame.size.height) 
    datePicked.frame = CGRectMake(datePicked.frame.origin.x, datePicked.frame.origin.y + 216, datePicked.frame.size.width, datePicked.frame.size.height) 
} 

编辑开始时,所有我的组件都被编程为移动。并在编辑后移回。手动编辑。是。