iOS 8 键盘隐藏了我的 TextView

标签 ios cocoa-touch uikeyboard uimodalpresentationstyle

我有一个 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/

相关文章:

html - iOS - 固定 Bootstrap 导航栏内容是完全透明的

ios - 什么 iOS 控件适合用于自定义可选列表?

objective-c - 在基于导航 Controller 的应用程序的最终 View 中隐藏或禁用后退按钮

iphone - 如何在全屏 OpenGL ES 应用程序上显示 iPhone/iPad 键盘

ios - 像 facebooks 菜单幻灯片一样拖动 uiview

javascript - 如何获取所有带有 "click"事件的元素与 jQuery 相关联?

ios - 如何在 iOS 中搜索姓名列表?

ios - 显示键盘时,Popover 内的 UIScrollView 内的 UITextView 不完全可见

iphone - UIKeyboardTypeDecimalPad 检测它是否有逗号 ","或点 "."

iphone - 当我禁用按键a-z时如何在iPhone键盘上启用按键“空格”和删除按键