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

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.

Related

iPhone: Make UIScrollView from UIView

I have a UIView with labels and textfields and I tested it in the simulator and noticed that I cannot scroll to see the rest of the view, how can I make the view into a UIScrollView and what do I need to do so I can make the view scrollable? I don't want to move labels and stuff or delete the UIView, is there a simple way?
Take a UIScrollView in Xib file outside your view. drag your view into this scrollview. The subviews inside your view remains same. and set the scrollview's contentSize

Moving UIButton from UIScrollView to UIView in iPhone SDK

I am stuck with a problem actually the scene is like this in my view controller i have place several buttons which i can move within the view now in same view half of the screens is occupied by uiscrollview in this scrollview also i have several uibuttons which i want to move from uiscrollview to uiview Now when i try to move uibutton from uiscrollview to uiview it hides as it moves from the scrollview similarly as i move uibutton from uiview to uiscrollview then also it hides as my drag reaches the scrollview area.
Please help me out with this problem
Thanks in advance....
Your UIButtons each have a superview. For the UIButtons in the scrollview, the UIScrollView is the superview. When you have scrollView.clipsToBounds == YES, then the UIButtons in the scrollview will become obscured if you move them outside of the visible area of the scrollview.
There are several possible solutions, including:
Add code to change the superview of the UIButtons once they reach the edge of the scrollview (but this is tricky, and I wouldn't do it unless there was an easier option, check out Apple's UIView documentation, especially (UIView)removeFromSuperview and (UIView)addSubview:). You would have to perform this switch of superview in the code that moves the button (or tracks the move).
Add the UIButtons to a UIView which is the parent of the scrollview, maybe even your viewcontroller.view (but your UIButtons in the scrollview will no longer move with the scrollview upon scrolling). You would add the buttons to the view behind the scrollview, but so that they show above it.

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.

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.