当键盘出现时,iOS 8会向上移动UIView问题

时间:2014-10-08 10:52:59

标签: ios objective-c iphone uiview ios8

我有一个UIViewUITextField位于屏幕底部,当键盘出现时会向上移动。

我在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

3 个答案:

答案 0 :(得分:2)

https://github.com/michaeltyson/TPKeyboardAvoiding

获取TPKeyboardAvoidingScrollView

使用如下。

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

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

答案 1 :(得分:1)

我不确定这是不是你的问题,但你应该使用基于块的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;

                     }
    ];
}

您可以阅读Apple的文档:

  

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

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

希望它可以帮到你!

答案 2 :(得分:0)

我使用了这段代码。它没有在滚动视图中向上移动。但改变了他们的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)
}

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