简体   繁体   English

键入一段时间后,UITextView消失在键盘后面

[英]UITextView disappears behind keyboard after typing a while

We're trying to support iOS 9 for a while yet... That's the only place this is happening... 我们暂时尝试支持iOS9。这是发生这种情况的唯一地方。

I have a UITextView that occupies the entire view, which occupies the entire screen. 我有一个UITextView ,它占据了整个视图,它占据了整个屏幕。 I register to receive UIKeyboardWillShowNotification and UIKeyboardWillHideNotification . 我注册接收UIKeyboardWillShowNotificationUIKeyboardWillHideNotification I get those just fine and adjust the height of my UITextView so that it is above the keyboard: 我得到的效果很好,并调整了UITextView的高度,使其位于键盘上方:

- (void) keyboardWillShow:(NSNotification *)aNotification 
    {
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect bounds = self.view.bounds;
    bounds.size.height = bounds.size.height - keyboardRect.size.height;
    self.textView.frame = bounds;
    }

Again, this works great. 再次,这很好。 Now I type a few lines. 现在我输入几行。 For a while, the bottom line of text is kept above the keyboard automatically as I add more lines of text. 有一阵子,当我添加更多行文本时,文本的底行会自动保持在键盘上方。 But eventually, usually within just a few lines (maybe 4-5), the next line will go under the keyboard and I can continue typing lines under the keyboard. 但是最终,通常在几行(可能是4-5行)之内,下一行将进入键盘下方,而我可以继续在键盘下方键入行。 Eventually the lines I can still see above the keyboard will start scrolling up as I hit the bottom of the screen with the lines I'm typing behind the keyboard. 最终,当我敲击屏幕底部的行时,在键盘上方仍然可以看到的行将开始向上滚动。

I am not getting the UIKeyboardWillHideNotification during this process, but it is almost behaving like I am -- that is, the size of my UITextView appears to return to its original size before the keyboard was shown. 在此过程中我没有得到UIKeyboardWillHideNotification ,但它的行为几乎与我一样-也就是说, UITextView的大小似乎恢复了显示键盘之前的原始大小。

All of this works fine in iOS 11. That is, as I type, the text scrolls up so the bottom line of text never goes behind the keyboard. 所有这些在iOS 11中都可以正常工作。也就是说,在键入时,文本会向上滚动,因此文本的底行永远不会落在键盘后面。 It's just iOS 9. Anybody remember a known issue back in those days, and a work-around? 只是iOS9。有人记得那些日子的已知问题,以及解决方法吗?

My best guess is that you're mixing Autolayout w/ Autoresizing and it seems to be picking the intended one in later iOS's but not in iOS 9. 我最好的猜测是,您正在混合使用带自动调整大小的Autolayout,并且似乎在以后的iOS平台中选择了预期的布局,但在iOS 9中却没有。

A quick sample using your code + auto layout constraints in the storyboard resulted in what I believe to be the behavior you're seeing in older iOS's (iOS 9.3 Simulator shown below): 在情节提要中使用您的代码+自动布局约束进行的快速采样导致我相信是您在较旧的iOS(以下所示的iOS 9.3模拟器)中看到的行为:

在自动+ AutoLayout_iOS9

If I roll with a pure auto layout approach (ie IBOutlet the textView's bottom constraint and adjust the constant for that programmatically in keyboardWillShow) then it results in the correct behavior on iOS 9 (no setFrame call): 如果我使用纯自动布局方法滚动(即IBOutlet textView的底部约束,并在keyboardWillShow中以编程方式调整该常量),则它将在iOS 9上产生正确的行为(不调用setFrame):

Pure_AutoLayout_iOS9

@property (strong, nullable) IBOutlet NSLayoutConstraint *textViewBottomConstraint;

...

- (void)keyboardWillShow:(NSNotification *)aNotification {
    CGRect keyboardRect = [[[aNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.textViewBottomConstraint.constant = -keyboardRect.size.height;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM