Developing for iPhone (and UIScrollView Example)
So, what have I been doing lately? Well I’ve been wrestling with my first Mobile App in Objective C and the Cocoa Touch frame work for every bodies’ favourite mobile platform!
Between a day job, a social life and a couple of other projects, I haven’t really had the time I need to really get to grips with iPhone as a development platform yet… one of the biggest things that is holding me (and I’m sure a lot of other developers) back is the limited documentation that’s available.
What you get from Apple with the SDK (both actual documents and help given by XCode’s research assistant) is very good but it suffers from being single sourced. That is to say that there are no other contributors in the periphery giving comments, suggestions or even pointing out mistakes with the official docs. This is partly because of the infamous iPhone SDK NDA (which fortunately as now being lifted) but it’s also down to the fact it’s a relatively new and proprietary technology…
A good example of this is the problem I had getting the UIScrollView class to work. The documentation was accurate to a ‘T’ and Apple even provided some fairly complex implementations to demonstrate how to use it with paging and zooming… but there was no example or explanation of how the fundamentals worked. I was left with the impression by the sample code that I needed to set up a delegate to handle all the events that the class actually does straight out the box! An hour later I was still playing around with implementations of the UIScrollViewDelegate protocol, trying to get my scroll bars to work when, all the while, all I needed to do was tell my UIScrollView the full dimensions of its content!
All I’d have needed to see was the 4 line example I’ve drawn up below, in the absence of Head First iPhone Development, I’ve even made a simple colour diagram showing the relationship between the two areas you have to specify! I doubt something so simple will help many people in the future, but you never know… and I hope this will be just the first example I write up for my blog!
//all variables are of type CGFloat, unless named otherwise!
UIScrollView *myScrollViewObj = [
[UIScrollView alloc] initWithFrame:
CGRectMake(frameX, frameY, frameWidth, frameHeight)
];
[myScrollViewObj setContentSize:
CGSizeMake(contentWidth, contentHeight)
];
[myParentViewObj addSubview:myScrollViewObj];
[myScrollViewObj release];
PS. Hey Steve, please don’t sue me! The NDA’s being lifted anyway right?
Comments
21 Responses to “Developing for iPhone (and UIScrollView Example)”
Leave a Reply
Thanks for this!
Thanks a lot for saving me from going crazy. Try changing a UIView to UIScrollView class in Interface builder. It ain’t working, especially not when simulating status bar and navigation bar.
Your “setContentSize” hint worked like a charm.
Thx, your/this blog post saved me a lot of time.
Oliver
Thanks a lot for your simplicity!
you save my day
Thanks.
Very helpful!
Simple and greate!
Thank you very much!
Awesome! Good job, just what we need.
It’s exactly this sort of simple getting started guides/examples that the official docs are missing (and that was also the idea behind the Flex Quick Start guides I wrote for Adobe). So in short, yes, it’s definitely useful and thanks for taking the time to write it up
Thanks for this! I’ve been struggling with this for a while. It always seems to be the odd hiccups between IB to Xcode and back that catch me out. I tried setting up the scrollview in IB and accessing it as an ivar in xcode to resize it, but it would never scroll for me. Odd thing is, setContentSize doesn’t help when used in the initWithNibName initializer, but works in viewWillAppear. Any thoughts why? Thanks again.
Thanks so much,
you saved my night !
Thanks a lot friend. This is what should be done.
Excellent !!!!!!!
Thanks! You saved me a ton of time. I had missed the point that initWithFrame gets the screen width, not the content width.
Thanks!!
This works like this also:
[scrollView addSubview:glView];
[window addSubview:scrollView];
Pretty easy to get an awesome scrolling effect!
Thanks…….
It simple as well as effective code to better
understandable.Your example figure are great!
Thanks …It is more simple and helpful. Save a day!!!!
Thank u……
Thanks !!!!!!!!
Please post on other topics too.
Wonderful post–thanks for taking the time to post this.
It was really amazing.
Simple straight and to the point….
Keep up good work.
Hi,
I just wrote this library, and this article helped me a lot
… so thanks for that
I just finished that, so if you’ll find anything odd about it, please let me know … cheers
http://www.xprogress.com/post-53-how-to-build-uiscrollview-with-page-control/
Ondrej
Many thanks.