iOS 8键盘隐藏我的textview [英] iOS 8 keyboard hides my textview
问题描述
我有一个 UIViewController
使用 self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;
现在在iOS 8最后 UITextView
在键盘上被隐藏,当它被推上来。如何避免?
I have a UIViewController
presented using self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;
and now in iOS 8 the last UITextView
is hidden from the keyboard when it will pushed up. How to avoid It?
推荐答案
@ Oleg的解决方案是好的,但它没有考虑到键盘更改的大小,显示..也,他硬编码动画持续时间和一些其他参数。请参阅下面的解决方案:
@Oleg's solution is good but it doesn't take into consideration keyboard changes in size while its already showing.. also, he hardcoded the animation duration and a few other parameters. See my solution below:
将 UIKeyboardWillChangeFrameNotification
的观察者添加到 viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
然后添加方法:
- (void)keyboardFrameWillChange:(NSNotification *)notification
{
CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.view.frame;
CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y);
self.view.frame = newFrame;
[UIView commitAnimations];
}
不要忘记删除viewDidUnload和Dealloc中的键盘观察器。
Don't forget to remove the keyboard observer in both viewDidUnload and in Dealloc.
这篇关于iOS 8键盘隐藏我的textview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!