2

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?

Community
  • 1
  • 1
Ric Levy
  • 966
  • 1
  • 15
  • 33

2 Answers2

1

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.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • I've tried doing this, but it makes the app hang as soon as I load the view. I think the property must not be set up yet when the `contentInset` method is called. Don't worry about the autocomplete - I'm actually trying to make the inset bigger, and only when the text is not being edited. When the keyboard appears, I want it to revert to 32px anyway. :) – Ric Levy May 25 '11 at 16:13
  • @RicLevy: That's odd. You could try accessing myContentInset via the ivar instead of the property, maybe that would fix it. – Anomie May 25 '11 at 16:44
  • It was just me - I was trying to return the built-in contentInset property instead of synthesizing my own. Works perfectly now. Thanks! – Ric Levy May 26 '11 at 08:02
0

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.

Emil
  • 7,220
  • 17
  • 76
  • 135
90k
  • 1