当键盘出现时,iOS 8会向上移动UIView问题 [英] iOS 8 move UIView up when keyboard appears | Issue

查看:93
本文介绍了当键盘出现时,iOS 8会向上移动UIView问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIView ,其中 UITextField 位于屏幕底部,当键盘会向上移动出现。

I have a UIView with a UITextField placed at the bottom of the screen which will move up when a keyboard appears.

我在iOS 8之前一直遵循以下方法,看起来效果很好。

I have been following the below method prior to iOS 8 and seems to work perfectly.

// 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。

But the above code doesn't seem to work in iOS 8 as the keyboard blocks the UIView behind it.

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

After a little research, I found out an almost-similar answer. But here the whole UIView was being pushed up, and what I would like to achieve was just to move the bottom UIView.

推荐答案

https://github.com/michaeltyson/TPKeyboardAvoiding

按如下方式使用它。

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

drop the TPKeyboardAvoidingScrollView.m and TPKeyboardAvoidingScrollView.h source files into your project, pop a UIScrollView into your view controller's xib, set the scroll view's class to TPKeyboardAvoidingScrollView, and put all your controls within that scroll view. You can also create it programmatically, without using a xib - just use the TPKeyboardAvoidingScrollView as your top-level view.

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

To disable the automatic "Next" button functionality, change the UITextField's return key type to anything but UIReturnKeyDefault.

这篇关于当键盘出现时,iOS 8会向上移动UIView问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆