如何阻止 UITextView 的底部边缘插入重置为 32?
我有一个全屏 UITextView,每当键盘出现时它就会变小,这样键盘就不会覆盖任何文本。作为其中的一部分,我还更改了 textView 的底部 contentInset,因此当存在键盘时文本下方的空间较小,而当没有键盘时文本下方的空间较大。
问题是,每当用户点击底部附近的 textView 开始编辑时,底部 contentInset 会自发地将其重置为 32。我从 这个答案可以子类化 UITextView并覆盖 contentInset
方法,如下所示:
@interface BCZeroEdgeTextView : UITextView
@end
@implementation BCZeroEdgeTextView
- (UIEdgeInsets) contentInset
{
return UIEdgeInsetsZero;
}
@end
但这并不能阻止底部插图自行重置 - 它只是更改其重置为的数字。如何使我的 UITextView 保持我设置的 contentInset 值?
I have a full-screen UITextView that gets smaller whenever the keyboard appears, so that the keyboard doesn't cover any of the text. As part of this, I also change the textView's bottom contentInset, so the space below the text is smaller when the keyboard is present, and larger when there is no keyboard.
The problem is, whenever the user taps the textView near the bottom to start editing, the bottom contentInset spontaneously resets itself to 32. I know from this answer that it is possible to subclass the UITextView and override the contentInset
method, like this:
@interface BCZeroEdgeTextView : UITextView
@end
@implementation BCZeroEdgeTextView
- (UIEdgeInsets) contentInset
{
return UIEdgeInsetsZero;
}
@end
But this doesn't stop the bottom inset resetting itself - it just changes the figure it resets itself to. How can I make my UITextView just keep to the contentInset value that I have set?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使其保持您设置的值,您可以走子类路线,但返回您自己的属性值而不是常量,如下所示:
但请注意, UITextView 将其底部 contentInset 重置为 32 的原因是更标准inset 将切断自动完成弹出窗口等。
To make it keep the value you set, you could go the subclass route but return the value of your own property rather than a constant, something like this:
But do note that the reason UITextView resets its bottom contentInset to 32 is that a more standard inset will cut off the autocompletion popups and such.
这是我的解决方案,但有点长:
子类
UITextView
并添加属性customInsets
,每当您需要设置contentInset
和scrollIndicatorInsets 时
,设置customInsets
来代替。Here is my solution, but a bit longer:
Subclass
UITextView
and add a propertycustomInsets
, whenever you need to setcontentInset
andscrollIndicatorInsets
, setcustomInsets
instead.