我有一个 UIViewController
使用 self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;
呈现,现在在 iOS 8 中,最后一个 UITextView
被隐藏了键盘向上推时。如何避免?
最佳答案
@Oleg 的解决方案很好,但它没有考虑键盘大小的变化,而它已经显示了。此外,他对动画持续时间和其他一些参数进行了硬编码。请参阅下面的解决方案:
将 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 中移除键盘观察器。
关于iOS 8 键盘隐藏了我的 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26213681/