如何在输入时阻止UITextView向上滚动

Ger*_*ero 29 iphone uitableview uitextview

我有一个UITextView包含在UITableViewCell.最初显示视图时,布局是正确的,但是一旦我点击UITextView它就会自动向上滚动一下,第一行上的字符的上半部分变得不可见.

此图像是UITextView未激活时:
UITextView未激活http://gerodt.homeip.net/uitextview-notactive.png

这个是当我点击UITextView它使其激活时:
UITextView活动http://gerodt.homeip.net/uitextview-active.png

我根本不向上滚动UITextView,它应该简单地保持固定.我怎样才能做到这一点?我已经尝试了几个设置Interface Builder,但到目前为止没有运气.

任何建议表示赞赏.

格罗

Bri*_*dos 62

UITextView是UIScrollView的子类,因此它具有可配置的contentInset属性.不幸的是,如果你尝试在UITextView实例上更改contentInset,那么底边插入总是被重置为32.我之前使用简短的UITextView框架遇到了这个问题,并发现这是一个问题.我怀疑这是导致问题的原因,但您应该在调试器中检查textview的contentInset以确定.

解决方法/解决方案很简单:子类UITextView并覆盖contentInset方法,以便它始终返回UIEdgeInsetZero.试试这个:

//
// BCTextView
//
// UITextView seems to automatically be resetting the contentInset
// bottom margin to 32.0f, causing strange scroll behavior in our small
// textView.  Maybe there is a setting for this, but it seems like odd behavior.
// override contentInset to always be zero.
//
@interface BCZeroEdgeTextView : UITextView
@end

@implementation BCZeroEdgeTextView

- (UIEdgeInsets) contentInset { return UIEdgeInsetsZero; }

@end 
Run Code Online (Sandbox Code Playgroud)


ero*_*ppa 16

这就是UITextView的行为,根据Apple的工程师的意图,UITextView适用于高度至少为几行的文本.没有解决方法,使用UITextField代替或将UITextView增加到至少3行高度.


Sno*_*man 6

你也可以这样做:

textView.contentInset=UIEdgeInsetsZero;
Run Code Online (Sandbox Code Playgroud)

在您的委托文件中.