Iphone 如何停止UITextView';s底边插入从重置到32?

Iphone 如何停止UITextView';s底边插入从重置到32?,iphone,cocoa-touch,uiscrollview,uitextview,Iphone,Cocoa Touch,Uiscrollview,Uitextview,我有一个全屏幕UITextView,每当键盘出现时,它就会变小,这样键盘就不会覆盖任何文本。作为这项工作的一部分,我还更改了textView的底部contentInset,因此当有键盘时,文本下方的空间更小,而当没有键盘时,文本下方的空间更大 问题是,每当用户点击底部附近的textView开始编辑时,底部contentInset就会自动重置为32。我从中了解到,可以对UITextView进行子类化,并覆盖contentInset方法,如下所示: @interface BCZeroEdgeText

我有一个全屏幕UITextView,每当键盘出现时,它就会变小,这样键盘就不会覆盖任何文本。作为这项工作的一部分,我还更改了textView的底部contentInset,因此当有键盘时,文本下方的空间更小,而当没有键盘时,文本下方的空间更大

问题是,每当用户点击底部附近的textView开始编辑时,底部contentInset就会自动重置为32。我从中了解到,可以对UITextView进行子类化,并覆盖
contentInset
方法,如下所示:

@interface BCZeroEdgeTextView : UITextView
@end

@implementation BCZeroEdgeTextView

- (UIEdgeInsets) contentInset 
  { 
  return UIEdgeInsetsZero; 
  }

@end
@interface BCCustomEdgeTextView : UITextView
@property (nonatomic, assign) UIEdgeInsets myContentInset;
@end

@implementation BCCustomEdgeTextView

@synthesize myContentInset;

- (UIEdgeInsets) contentInset { 
    return self.myContentInset; 
}

@end

但这并不能阻止底部插图自身的重置——它只是改变了其自身重置为的图形。如何使我的UITextView只保留我设置的contentInset值?

要使其保留您设置的值,您可以通过子类路径,但返回您自己属性的值,而不是常量,如下所示:

@interface BCZeroEdgeTextView : UITextView
@end

@implementation BCZeroEdgeTextView

- (UIEdgeInsets) contentInset 
  { 
  return UIEdgeInsetsZero; 
  }

@end
@interface BCCustomEdgeTextView : UITextView
@property (nonatomic, assign) UIEdgeInsets myContentInset;
@end

@implementation BCCustomEdgeTextView

@synthesize myContentInset;

- (UIEdgeInsets) contentInset { 
    return self.myContentInset; 
}

@end

但请注意,UITextView将其底部内容inset重置为32的原因是,更标准的inset将切断自动完成弹出窗口等。以下是我的解决方案,但要长一点:

- (void)setCustomInsets:(UIEdgeInsets)theInset
{
    customInsets = theInset;
    self.contentInset = [super contentInset];
    self.scrollIndicatorInsets = [super scrollIndicatorInsets];
}

- (void)setContentInset:(UIEdgeInsets)theInset
{
    [super setContentInset:UIEdgeInsetsMake(
        theInset.top + self.customInsets.top,
        theInset.left + self.customInsets.left, 
        theInset.bottom + self.customInsets.bottom, 
        theInset.right + self.customInsets.right)];
}

- (void)setScrollIndicatorInsets:(UIEdgeInsets)theInset
{
    [super setScrollIndicatorInsets:UIEdgeInsetsMake(
        theInset.top + self.customInsets.top, 
        theInset.left + self.customInsets.left, 
        theInset.bottom + self.customInsets.bottom, 
        theInset.right + self.customInsets.right)];
}

子类
UITextView
并添加属性
customInsets
,每当需要设置
contentInset
ScrollIndicationSets
时,改为设置
customInsets

我已经尝试过这样做,但一旦加载视图,应用程序就会挂起。我认为在调用
contentInset
方法时,还不能设置属性。不必担心自动完成-我实际上是想让插入的内容变大,而且只有当文本没有被编辑时。当键盘出现时,我希望它恢复为32px@里克莱维:真奇怪。您可以尝试通过ivar而不是属性访问MyContentSet,也许这会解决它。只有我-我尝试返回内置contentInset属性,而不是合成我自己的属性。现在效果很好。谢谢