[英]Problems 'Resizing' UITextView when the Keyboard is Shown, in iOS 7
I'm targeting iOS7 only. 我仅针对iOS7。
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. 我想在显示键盘时“调整”
UITextView
的大小,以便可以看到所有文本,而不是隐藏在键盘后面。
I've considered a few different approaches to this... 我考虑过几种不同的方法...
1) Change the frame of the UITextView
when the keyboard shows. 1)当键盘显示时,更改
UITextView
的框架。
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
的范围,因此不可见:
UITextView cursor below frame when changing frame 更改框架时,UITextView光标位于框架下方
You can see this effect from the following screen shot. 您可以从以下屏幕截图中看到这种效果。 The
UITextView
has a green background. UITextView
具有绿色背景。 It's been added to a UIView
with a red background. 它已添加到带有红色背景的
UIView
。 The arrow shows where the cursor is... 箭头显示光标所在的位置...
2) Changing the contentInset
property on the UITextView
2)更改
UITextView
的contentInset
属性
I believe that this recommended/preferred approach. 我相信这种推荐/首选方法。 Note, I've read the Apple documentation for resizing views based on the keyboard appearing/disappearing:
注意,我已经阅读了Apple文档,以根据键盘的出现/消失来调整视图的大小:
https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7 https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7
With my code, I'm getting no effect when I change the bottom component of the UIEdgeInsets
. 使用我的代码,更改
UIEdgeInsets
的底部组件不会产生任何效果。
Same example as above, green UITextView
on a red UIView
, the text disappears underneath the keyboard: 与上面相同的示例,绿色
UITextView
在红色UIView
,文本在键盘下方消失:
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 . 注意 :
scrollIndicatorInsets
部分工作正常 。 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
3)更改
UITextView
上的textContainerInset
The answer on this question suggests using textContainerInset
instead of contentInset
on iOS 7: 关于此问题的答案建议在iOS 7上使用
textContainerInset
而不是contentInset
:
UITextView contentInset not working in UITextView on iOS 7? UITextView contentInset无法在iOS 7的UITextView中使用?
I've tried this also, but still don't manage to resize the UITextView
. 我也尝试过,但是仍然无法调整
UITextView
大小。
In this question, 'mann' is also having problems with both the contentInset
and the textContainerInset
: 在这个问题中,“ mann”在
contentInset
和textContainerInset
也都存在问题:
UITextView content Inset Bottom not working iOS7 UITextView内容插入底部无法正常运行iOS7
Questions 问题
textContainerInset
or contentInset
? textContainerInset
或contentInset
? contentInset
, am I missing something? contentInset
的代码中,我缺少什么吗? Is there something else I need to set? Thanks 谢谢
It's a bug in iOS 7. Pete Stenberger solved this with a subclass of UITextView. 这是iOS 7中的错误。PeteStenberger用UITextView的子类解决了这个问题。 You can find it here:
你可以在这里找到它:
https://github.com/steipete/PSPDFTextView https://github.com/steipete/PSPDFTextView
Here's more info: 这是更多信息:
http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/ http://petersteinberger.com/blog/2014/fixing-uitextview-on-ios-7/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.