Seems like a very simple problem! But I'm having great difficulty.
My ideas and attempts so far:
scrollView.backgroundColor = [UIColor colorWithPatternImage:myImage]]- Doesn't scroll with contents
- Create
UIViewthe size ofcontentSize(actually, larger due to elastic bounces on scroll view), and use pattern as above on this sub view ofUIScrollView.- Causes massive memory hit (and crash) when UIView gets too large. See also: Large UIScrollView with background pattern fails
- Similar to (2), exept I only create a
UIViewthe size of the maximum number of repetitions of the background image that will be seen, and cleverly move the view to wherever is needed to show the correct background repetitions.- Causes this background view to shift left and right unexpectedly when animating the scroll.
- Create
UIViewsubclass and overridedrawRect. Use various Core Graphics techniques to draw content by hand.- Not animatable. Implementing my own
contentOffsetproperty isn't a standard Core Animation property. - Overriding
drawRectonUIScrollViewdoesn't respect the content offset, and doesn't get called multiple times as it scrolls/animates. The rect parameter is always simply the bounds of theUIScrollView.
- Not animatable. Implementing my own
- As with (4), except I set the
bounds.originof myUIViewsubclass in mysetContentOffsetimplementation, since it's an animatable property.drawRectdoesn't seem to get called every frame.
- Use
CATiledLayer, as suggested in this answer: Large UIScrollView with background pattern fails. Implementation details here: http://www.cimgf.com/2011/03/01/subduing-catiledlayer/.- I really don't want the ugliness of seeing tiles asynchronously being drawn as user scrolls. It's just a simple background pattern!
This seems like the simplest thing! Why is it so hard!?