I'm stuck with some kind of magic: when I try to change UITextView frame (in this case with UISlider), text is drawn in some other (smaller) area than frame (resizing multiple times). The funny thing is that if we slide fast enough when trying to make frame bigger, text is drawn in pretty correct area. Can somebody explain why? I tried to layout, autoresize, set content size and bounds, but nothing works.
- (void)viewDidLoad
{
[super viewDidLoad];
_textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
_textView.backgroundColor = [UIColor greenColor];
_textView.text = @"some useless text which will be drawn bad";
[self.view addSubview:_textView];
UISlider *slide = [[UISlider alloc] initWithFrame:CGRectMake(10, 130, 100, 20)];
[slide setValue:0.0];
[slide addTarget:self action:@selector(changedValue:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:slide];
}
- (void)changedValue:(UISlider *)slider
{
CGRect textViewFrame = _textView.frame;
textViewFrame.size.width = [slider value] * 200;
_textView.frame = textViewFrame;
}
before resize
after resize
Well, after several hours of google, I find out that setting CGRectZero frame before setting my calculated frame value eliminates my problem. (I subclassed UITextView to override setFrame: method). My code bellow.
- (void)setFrame:(CGRect)frame
{
[super setFrame:CGRectZero];
[super setFrame:frame];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With