UIScrollView scrolling not proper - iphone

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

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.

Related

Can't override "hitTest" method for UIScrollView

I've created a scroll view, width less than the screen width and set clipsToBounds=YES. As the UIScrollView doesn't scroll from anywhere outside the range of its frame, I put the UIScrollview inside a UIView and tried to override the "(UIView *)hitTest:WithEvent:" method. But it always shows warning pointInside method not found (or something like that) in the if ([self pointInside:point withEvent:event]) line and it doesn't work. What did I do wrong? Thanks in advance...
You have to change the ScrollView's contentSize in order to allow the ScrollView to scroll. It will only scroll it the contentSize.x is bigger than scrollview.size.x and/or contentSize.y bigger than scrollview.size.y.
Answering my own question, the problem is I didn't create the UIVIew subclass to override the (UIView *)hitTest:WithEvent: while the UIScrollView was in a subview of self.view. So, it is important to check which view inherits the scroll view and write overridden method in the parent view class.

How to use drawRect with a subclass of UIScrollView?

I'm trying to implement an owner-drawn view that is a subclass of UIScrollView. Basically, I want to custom-draw the contents of the view, and then have the stuff I've drawn be scrollable.
I've overridden drawRect in my view and I'm able to draw my contents, scaled to the size of the UIScrollView's contentSize property (so some of my custom drawing is not visible, as I intended).
The problem is that the content then never moves. I can drag my finger up and down, and this makes the UIScrollView's scrollbars appear, but my custom-drawn content never moves or changes - I still always only see the top half.
How can I custom-draw content for this UIScrollView so that what I've drawn is scrollable?
Are you calling [super drawRect]; at the end of your own drawRect method?
Edit
I misread the question. You'll need to create your own UIView subclass and put your overridden drawRect in that. Then, add that view as a child of the UIScrollView.

Any way to draw Core Graphics in the contentView of a UIScrollView

I was wondering if it was possible to draw any core graphics in the content view of a scroll view because at the moment, in the drawRect section of the UIScrollView, it just draws on the static view and when I scroll the content view, the graphics stay still... (which means I'm not drawing in the right view)
Do I have to create another UIView subclass and draw there and add it to the UIScrollView, while handling the zooming and scrolling from the UIScrollView
or - is there a way I can keep it all in the one subclass of UIScrollView!
(I hope that makes sense!) thanks!
Typically, you don't draw in a UIScrollView directly. UIScrollView is just a container view which scrolls one content view or several tile views. You should add the content view(s) with addSubview: and position them as needed. The scroll view will handle scrolling and zooming.

UIScrollView and CALayers

I have a custom UIView that is composed entirely of CALayers.
In the awakeFromNib method it creates and sets all the CALayers into their appropriate positions (CAGradientLayer, several CATextLayers, and a few custom CALayer subclasses). The custom UIView does not override the drawRect: method because there's no drawing done directly into the view (all of the drawing is done in the sublayers).
So I took this view and embedded it in a UIScrollView. The problem? No scroll bars appear and the view does not scroll. The view is clearly larger than the bounds of the scroll view, and instead of allowing me to scroll, it just cuts off at the scroll view bounds.
What could be wrong here?
You have to set the scrollView's contentSize.

Drawing a shadow below a UIScrollView (that scrolls a UIView)

My setup is a UIScrollView in the center of the screen (on the iPhone - like 300x400 positioned in the center) that contains a UIView of the same width, so it scrolls it vertically. In this UIView i draw custom subviews with labels etc (it's a scoreboard with various colors).
What i'd like to have is some shadow below my UIScrollView, so that the whole scrolling scoreboard floats over my background.
I have found this nice post
How do I draw a shadow under a UIView?
I use this code in my ScrollView subclass but it doesn't work for me. Maybe because I don't draw the actual shapes in the ScrollView's drawRect: (since they are drawn on the UIView).
Also I guess that in order to have the View scroll in the ScrollView and the shadow of the ScrollView outside the scrolling area, I guess I should extend the "bounds" of the ScrollView, right?
It's not quite clear to me what you're asking but, if you want the scrollView contents to scroll over a static image you simply need to add a UIView (or more likely a UIImageView) to your superview and then add your UIScrollView to that. If you set he background colour of the UIScrollView to be celarColor, the background image will show through - so you have a view heirarchy like:
UIWindow
UIView <----- your background here
UIScrollView
Scrolling subviews <----- high score table here
If you draw your highscore table in the scrolling subviews using CoreGraphics, the answer in the question you linked to will also work.
How about explicitly filling the entire self.bounds rectangle in your scroll view subclass' drawRect: method before calling super?
Another idea is to put the scroll view inside of another view which does the shadow drawing.