Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

buggy margin behavior with dynamically resizing UITextView

I need a dynamically resizing UITextView but the right margin encroaches towards the left alarmingly after numerous resizes so that a very narrow strip of text is shown with lots of white space in the text view. This can be reproduced by a simple setup with just a UITextView and UISlider. A simple sample setup that produces this behavior is UISlider with value range from 0-200, a UITextView of 320 width and this code:

- (IBAction)sliderValueChanged:(UISlider *)slider {
    textView.frame = CGRectMake(0, 0, 320 - slider.value, 300);
}

Some things I've tried are tinkering with the autoResizingMask, contentMode, contentOffset, and sizeToFit but none of them work. How can this weird behavior be avoided, or is it a bug?

like image 362
richard Avatar asked Jan 23 '26 11:01

richard


1 Answers

Subclass UITextView and override layoutSubViews to set the frame to CGRectZero and then back to original frame size. It's not elegant but it works.

-(void) layoutSubviews
{
    CGRect rect = [self frame];
    [self setFrame:CGRectZero];
    [self setFrame:rect];
}
like image 161
cbunn Avatar answered Jan 26 '26 02:01

cbunn