Hello, I have a UIViewController subclass which has the view property set to a UIScrollView subclass (say ProfileScrollView). Now, I have overridden drawRect: method, in which I draw a lot of text and place UIButtons as subview in between texts. Depending on the text data, I set contentSize of the scrollView in this drawRect: method. Now, when I scroll this view the buttons are scrolling but the text remains stagnant. Please help with this problem. Thanks
+1
A:
Hi, I think your problem comes from the fact that you use drawRect:
for your text. I'm pretty sure the positioning given by the CGRect argument is absolute and not depending on the scrollView's content's current offset.
Did you add your subviews (buttons and text) directly in the UIScrollView? If you did, try to insert a simple UIView in UIScrollView, which will contain all subviews. Should be something like this:
-> UIScrollView
-> UIView
-> UIButtons
-> Text
-> ...
So drawInRect:
will draw the text in containing UIView with an absolute position, and UIScrollView will make UIView (and consequently all its contents) scroll.
Jukurrpa
2010-07-10 10:22:16
Thanks for answering. I think, This is the required solution. But why it doesn't scroll the text as I have overriden drawRect: of scrollView?
2010-07-10 13:13:03
Did you try it, and did it effectively work? Because I had some doubts after posting my answer. But I guess the reason is you draw your text using NSString's drawInRect or drawAtPoint, which place text in an absolute position. UIButton (and other UIViews) are placed relatively to their superview, so UIScrollView can alter their positioning according to where the user scrolled.
Jukurrpa
2010-07-10 20:49:29
Yes, your solution worked. However, regarding my previous approach, I placed a breakpoint inside drawRect: and when I was scrolling the scrollView, drawRect: wasn't called. So this means it was not drawing the string again and again at the same absolute point, rather it has drawn it in a constant view (not scrollView).
2010-07-11 15:51:05