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!





uiscrollview diagram


//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)”

  1. Paul on November 11th, 2008 6:55 am

    Thanks for this!

  2. Lutz on November 30th, 2008 10:49 pm

    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.

  3. Oliver on December 11th, 2008 8:47 pm

    Thx, your/this blog post saved me a lot of time.
    Oliver

  4. Maz on February 19th, 2009 6:09 pm

    Thanks a lot for your simplicity!
    you save my day

  5. Gal on March 1st, 2009 11:29 am

    Thanks.
    Very helpful!

  6. Sergey on March 8th, 2009 6:07 pm

    Simple and greate!

    Thank you very much!

  7. you're the man on May 20th, 2009 8:00 pm

    Awesome! Good job, just what we need.

  8. Aral Balkan on June 20th, 2009 11:53 am

    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 :)

  9. Shaun Cullen on June 26th, 2009 9:25 am

    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.

  10. Stéphan Barbé on June 30th, 2009 9:24 pm

    Thanks so much,
    you saved my night !

  11. Ruchira on July 18th, 2009 5:31 pm

    Thanks a lot friend. This is what should be done.
    Excellent !!!!!!!

  12. Andy on August 3rd, 2009 1:59 am

    Thanks! You saved me a ton of time. I had missed the point that initWithFrame gets the screen width, not the content width.

  13. Ryan Behr on November 11th, 2009 5:34 am

    Thanks!!

    This works like this also:
    [scrollView addSubview:glView];

    [window addSubview:scrollView];

    Pretty easy to get an awesome scrolling effect!

  14. sudesh kumar on November 25th, 2009 7:41 pm

    Thanks…….
    It simple as well as effective code to better
    understandable.Your example figure are great!

  15. Allen on November 30th, 2009 6:25 am

    Thanks …It is more simple and helpful. Save a day!!!!
    Thank u……

  16. Sachin on December 17th, 2009 12:48 pm

    Thanks !!!!!!!!
    Please post on other topics too.

  17. Bart Hickman on December 25th, 2009 9:03 pm

    Wonderful post–thanks for taking the time to post this.

  18. Sai on March 8th, 2010 7:54 am

    It was really amazing.

  19. Panks on March 16th, 2010 9:39 am

    Simple straight and to the point….

    Keep up good work.

  20. Ondrej on April 12th, 2010 6:31 pm

    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

  21. Dinesh on May 3rd, 2010 8:57 am

    Many thanks.

Leave a Reply