views:

196

answers:

2

I have a UIView inside of a UIScrollView that I want to resize.

I can increase the height easily by :

CGRect frame = self.drawinView.frame;
frame.size.height += 100;
self.drawinView.frame = frame;
self.drawinScrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height);

And all is good.

The above code will create a new area of the view at the bottom of the view that I can fill out.

Now when I resize the view, I only want to be repainting the new portion of the view that has just been created. I dont want to have to repaint the whole view.

However! I have run into difficulty when I need to expand the top of the view.

Doing :

CGRect frame = self.drawinView.frame;
frame.origin.y -= 100;
self.drawinView.frame = frame;
self.drawinScrollView.contentSize = CGSizeMake(500, 600);

does not work.

How can I do this without having to repaint the entire view?

A: 

In the above code you are only changing the origin of the view. Not it's size.

St3fan
I can change its size as well : frame.size.height += 100; this does not work. It adds an area to the bottom of the view still.
Mongus Pong
+1  A: 

You can set the content size to any large number you want, but you can never scroll further left or up than the top-left corner at 0,0. If you have a view with y at -100, it will not let you scroll to the 100px above 0.

Instead, you need to leave it where it is and instead set the contentOffset to +100 vertically.

If, for example, you want to resize the view by 100px up, you would do this:

CGRect frame = self.drawinView.frame;
frame.size.height += 100;
self.drawinView.frame = frame;
self.drawinScrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
self.drawinScrollView.contentOffset = CGPointMake(0,100);

You would some how have to manually tell the drawinView that you want to lock the contents to the bottom of the view instead of the top.

Ed Marty
Ok, that makes sense.. I suspect this telling drawinView to lock the contents to the bottom would involve getting it to repaint the whole canvas? I was hoping to avoid this.. can you think of another way?
Mongus Pong
I'm not sure about any of the following: There's the contentMode property of the UIView class. If you set it to UIViewContentModeBottom, then when you resize the view, it should automatically call repaint and maybe it will only pass in the dirty rect for the area revealed on top. If not, you could always just only draw that area and not clear the context before drawing. (view.clearsContextBeforeDrawing = false)
Ed Marty
Ok, I didnt try that.. I am accepting the bit of your answer that just said you cant set -ve numbers! What I have done instead is just work out the maximum canvas size I will need in the whole scenario and then just limit the scrolling manually.. It gives the same effect and probably makes it a bit snappier! Will look into this contentMode when I get the time. Thanks for the help! 8^)
Mongus Pong