iOS 7中显示键盘时“调整大小”UITextView的问题 [英] Problems 'Resizing' UITextView when the Keyboard is Shown, in iOS 7
问题描述
我的目标只是iOS7。
我想在键盘输入时调整大小 UITextView
显示,以便可以看到所有文本,而不是隐藏在键盘后面。
我已经考虑了几种不同的方法...
1)当键盘显示时,更改 UITextView
的框架。
以下问题详述了我使用此方法时遇到的同样问题 - 尽管正确设置了帧,但最后一行/游标将超出的范围UITextView
,因此不在视线范围内:
With我的代码,当我更改 UIEdgeInsets
的底部组件时,我没有任何效果。
同样的例子如:上面,绿色 UITextView
红色 UIView
,文字消失在键盘下方:
这是代码:
- (void)keyboardWillShow:(NSN otification *)通知
{
CGSize keyboardSize = [[[user userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] .size;
UIEdgeInsets insets = _textView.contentInset;
insets.bottom + = keyboardSize.height;
_textView.contentInset = insets;
_textView.scrollIndicatorInsets = insets;
}
注意: scrollIndicatorInsets
part 正常工作。用屏幕截图很难描绘,但滚动指示器在正确的位置开始和停止并且看起来是正确的大小。
我读了一堆人们遇到类似问题的问题。
3)在<$ c上更改 textContainerInset
$ c> UITextView
这个问题的答案建议使用 textContainerInset
而不是iOS 7上的 contentInset
:
UITextView contentInset在iOS 7上的UITextView中不起作用?
我也试过这个,但仍然无法调整 UITextView
的大小。
在这个问题上,'mann'也遇到了 contentInset
和 textContainerInset
的问题:
UITextView内容Inset Bottom无效iOS7
问题
- 这是正确/首选方法,
textContainerInset
或contentInset
? - 在上面显示的代码中设置
contentInset
,我错过了什么?我还需要设置其他内容吗? - 这些是iOS 7的错误吗?
谢谢
这是iOS 7中的一个错误.Pete Stenberger用UITextView的子类解决了这个问题。你可以在这里找到它:
https:// github。 com / steipete / PSPDFTextView
以下是更多信息:
http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/
I'm targeting iOS7 only.
I want to 'resize' a UITextView
when the keyboard is shown, so that all of the text can be seen, rather than being hidden behind the keyboard.
I've considered a few different approaches to this...
1) Change the frame of the UITextView
when the keyboard shows.
The following question details the same problem that I have with this approach - despite the frame being set correctly, the last line/cursor will extend beyond the bounds of the UITextView
, and therefore be out of sight:
UITextView cursor below frame when changing frame
You can see this effect from the following screen shot. The UITextView
has a green background. It's been added to a UIView
with a red background. The arrow shows where the cursor is...
2) Changing the contentInset
property on the UITextView
I believe that this recommended/preferred approach. Note, I've read the Apple documentation for resizing views based on the keyboard appearing/disappearing:
With my code, I'm getting no effect when I change the bottom component of the UIEdgeInsets
.
Same example as above, green UITextView
on a red UIView
, the text disappears underneath the keyboard:
And here's the code:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets insets = _textView.contentInset;
insets.bottom += keyboardSize.height;
_textView.contentInset = insets;
_textView.scrollIndicatorInsets = insets;
}
Note: the scrollIndicatorInsets
part works fine. It's hard to depict with a screen shot, but the scroll indicator starts and stops at the right place and appears to be the correct size.
I've read a bunch of questions where people have had a similar problem.
3) Changing the textContainerInset
on the UITextView
The answer on this question suggests using textContainerInset
instead of contentInset
on iOS 7:
UITextView contentInset not working in UITextView on iOS 7?
I've tried this also, but still don't manage to resize the UITextView
.
In this question, 'mann' is also having problems with both the contentInset
and the textContainerInset
:
UITextView content Inset Bottom not working iOS7
Questions
- which is the correct/preferred approach,
textContainerInset
orcontentInset
? - in the code shown above for setting the
contentInset
, am I missing something? Is there something else I need to set? - are these bugs with iOS 7?
Thanks
It's a bug in iOS 7. Pete Stenberger solved this with a subclass of UITextView. You can find it here:
https://github.com/steipete/PSPDFTextView
Here's more info:
http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/
这篇关于iOS 7中显示键盘时“调整大小”UITextView的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!