iPhone/Objc — Programmatically Create Scrollview Without Interface Builder

I’m new to Objective C in general, and am having trouble finding useful docs with step by step examples like the django project (best docs in the world!).

I could find endless resources on building UIScrollViews through the Interface Builder, but not programmatically.

My biggest hurdle was: How do I subclass a UISCrollView method call (like viewForZoomingInScrollView) if I’m not using a UIScrollView class?

You simply need to set the delegate for the scrollView to point to whichever class HAS those methods.

Example:

// in my .m file
-(void)loadView {
    CGRect fullScreenRect = [[UIScreen mainScreen] applicationFrame];
    scrollView = [[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.delegate = self;
    self.view = scrollView;
}
// now my scrollView is loaded and ready to be populated with a scrollable subframe in viewDidLoad

// because the delegate has been set to 'self', these methods will be called despite this class not being a UIScrollView subclass.
-(UIView*)viewForZoomingInScrollView:(UIScrollView*) scroll  {
    return imageView;
}


One thought on “iPhone/Objc — Programmatically Create Scrollview Without Interface Builder

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s