Alan
Alan

Reputation: 9471

UITextView not resizing correctly

I have a view that has a table view on the top, and a scroll view below the table view. When I press the resize bar button item, I want to hide the table view and maximize the scroll view. I got the scroll view and table view to animate correctly, but I am trying to resize the UITextView inside the scroll view to take advantage of the extra screen space.

Whenever I calculate the resize, the UITextView goes to the top left corner of the screen, and I'm not sure why. I am not even modifying the X and Y, just the height.

CGRect newDesFrame = descriptionTextView.bounds;
newDesFrame.size.height = newDesFrame.size.height + tableViewFrame.size.height;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.5];

self.scrollView.frame = scrollFrame;
self.descriptionTextView.frame = newDesFrame;

[UIView commitAnimations];

I am not sure why this happens. Does the descriptionTextView.bounds get messed up since it's in a UIView inside a UIScrollView? It seemed that, when I do a NSLog of the X and Y of the scroll view, it's 0,0. It's weird since it's not at 0,0 in the superview, or in the view. How do I fix this?

Upvotes: 0

Views: 332

Answers (2)

Tim
Tim

Reputation: 226

descriptionTextView is most likely jumping to the top left because you are, in fact, changing the origin (x and y). You are starting with:

CGRect newDesFrame = descriptionTextView.bounds;

Getting the bounds of that text view will give you a CGRect with an origin of 0,0, as 'bounds' gives you the view's rectangle in its own, local coordinate space.

Try this instead:

CGRect newDesFrame = descriptionTextView.frame;

This will give you the view's rectangle in its superview's coordinate space, including the actual origin.

Upvotes: 1

Bogdan Andresyuk
Bogdan Andresyuk

Reputation: 523

It happens because you are doing self.descriptionTextView.frame=newDesFrame ( which is Bounds property, not frame!! To read about differences Link)

Solution:

self.descriptionTextView.bounds=newDesFrame

Upvotes: 1

Related Questions