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 edit开发者_Go百科ing, 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?
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:
@interface BCCustomEdgeTextView : UITextView
@property (nonatomic, assign) UIEdgeInsets myContentInset;
@end
@implementation BCCustomEdgeTextView
@synthesize myContentInset;
- (UIEdgeInsets) contentInset {
return self.myContentInset;
}
@end
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.
Here is my solution, but a bit longer:
- (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)];
}
Subclass UITextView
and add a property customInsets
, whenever you need to set contentInset
and scrollIndicatorInsets
, set customInsets
instead.
精彩评论