UIScrollView and CALayers - iphone

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.

Related

Overlaying a UIScrollview without cutting off touch events to the scrollview

I have a transparent overlay that I'd like to put over a UIScrollview. I'm adding it as an Imageview sibling view to the scrollview so that it remains stationary while the scrollview subviews move freely underneath. The problem is that views pass their events to the superview, not the siblings. IS there a way to pass events from this overlay to the scrollview? Or can anyone think of a better way to achieve the same effect? Thanks!
This should Just Work, as long as the UIImageView has its userInteractionEnabled property set to NO: the superview sends -hitTests:withEvent: to its subviews in order, and the UIImageView should return nil, whereas the UIScrollView should return itself (because it has gesture recognizers).
If it's not working for you, the chances are that your view layout is not what you think it is. UIView has a useful method called -recursiveDescription which you should call on the superview and NSLog the result.

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.

How should child views of UIScrollView report their bounds for contentSize?

I'm looking more for advice on the correct design for a view.
What I have is a UIScrollView that contains one or more custom Views I have created. My problem is, who reports to the scrollview what it's contentSize should be? I have the following:
UIView
+-UIScrollView
+-CustomView 1 with dynamic height depending on data
+-CustomView 2 with dynamic Height depending on data
The UIViewController creates new instances of the custom views with data and then adds them as subviews to the UIScrollView. The problem I'm having is how to set the value of the scrollview's contentSize? Right now, I'm not doing that and the contents of the scrollview are clipped with no scrolling possible.
Should the custom view call [parent setContentSize:] in its drawRect:?
Should the UIViewController query the custom view after creation to get its bounds and then call setContentSize?
Should I subclass the UIScrollView to override addSubView to query each subview's height?
Is there something else I'm missing?
I hope I explained that properly. I'm new to this and still getting a handle on things.
The contentSize of the scroll view should be the size of the union of the frames of all your custom views. Whenever the size of a custom view changes, or one is added or removed, the view controller should calculate the new contentSize and apply it.
Setting it from drawRect: could essentially set up an infinite loop.
Using the bounds does not give the coordinates within the parent view.
You could subclass UIScrollView if the custom views do not change size.

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.