0

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: Pricing Grid

Bob Spryn
  • 17,742
  • 12
  • 68
  • 91

2 Answers2

0

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?

clstroud
  • 143
  • 8
  • Hmm.. that seems a little hacky. I'll wait a bit to see what other suggestions come up. Thanks! – Bob Spryn Apr 03 '12 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 03 '12 at 22:35
  • One other thing though, are you using init or initWithFrame: to initialize your custom UIView? – clstroud Apr 03 '12 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 04 '12 at 01: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 04 '12 at 01:13
-1
  • 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.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
  • I just thought of this same thing, but you confirming it makes me think its right. :) Thanks! – Bob Spryn Apr 04 '12 at 05:33
  • `layoutSubviews` gets called more than once. See: http://stackoverflow.com/a/20131528/143225 – Brenden Nov 21 '13 at 20:50
  • Yes, you do need to be careful in -layoutSubviews and only do the work if you need to. That's why I said "check if the view's desired size has changed". If -layoutSubviews is called again, but nothing has changed, then you don't need to do anything. – Kurt Revis Nov 22 '13 at 00:48