Don't know the final frame size of UIView until after drawRect?

The key concept is: Separate the ideas of "determine how large my view's stuff is" and "draw my view's stuff". Right now, you are doing the size computation while you draw, but you need to be able to do it earlier.

Up vote 0 down vote favorite share g+ share fb share tw.

I'm drawing a grid of data in a UIView with drawRect, of which I won't know the final size when the UIView is created because the number of columns and rows is dynamic. Sure I could do the calculations before creating the UIView, but that doesn't really make sense, because I'll also be doing those calculations in the UIView subclass, and would rather not have to extract that. So how do I handle this?

Do I init with a very large frame and adjust it after drawRect is done? I will also be setting this view as the content of a UIScrollView in case its too large to be viewed in the area allotted for it. The view I'm going to be drawing looks something like this: ios core-graphics link|improve this question asked Apr 3 at 20:41Bob Spryn1,023716 71% accept rate.

Add a method in your view that determines how large it should be, based on its data. This needs to be separate from drawRect:. In your view controller, when you're setting up the view, call that method to get that size.

Then set your view's bounds and/or frame to that size. And also set the scroll view's contentSize to that size. The key concept is: Separate the ideas of "determine how large my view's stuff is" and "draw my view's stuff".

Right now, you are doing the size computation while you draw, but you need to be able to do it earlier. If you want to get fancy, you could look into overriding layoutSubviews in a superview of your view -- that would be a good place to check if the view's desired size has changed, and then update the view's size and the scroll view's contentSize. But it isn't necessary to do that to start.

I just thought of this same thing, but you confirming it makes me think its right. :) Thanks! – Bob Spryn Apr 4 at 5:33.

Perhaps others have another solution but I think that in your case I would override setFrame: and call setNeedsDisplay so that drawRect will be called after your frame changes. Have you tried this approach already?

Hmm.. that seems a little hacky. I'll wait a bit to see what other suggestions come up. Thanks!

– Bob Spryn Apr 3 at 22:31 Perhaps... but I believe you are expecting it to work more like layoutSubviews, which is called every time the frame changes. DrawRect will only be called when something specifically asks it to re-paint the view. – clstroud Apr 3 at 22:35 One other thing though, are you using init or initWithFrame: to initialize your custom UIView?

– clstroud Apr 3 at 22:36 I'm not using either yet. I was confused at to how to handle that when I don't know my frame to start with. – Bob Spryn Apr 4 at 1:00 Ah okay so if you don't know the view's frame at all when instantiating, I still think your only options are going to be to call setNeedsDisplay from an overridden setFrame: or perhaps call it from the parent view controller's layoutSubviews method (not recommended).

Otherwise I'm curious to see what others suggest! – clstroud Apr 4 at 1:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions