MOBILE

iPhone Application Development : Using Advanced Interface Objects and Views - Using Scrolling Views

2/19/2011 3:38:31 PM
A third way that we can fit more into a single view—by making it scroll. Using an instance of the UIScrollView class, you can add controls and interface elements out beyond the physical boundaries of the iPhone screen. Unfortunately, Apple provides access to this object in Interface Builder but leaves out the ability to actually make it work.

Before closing out this hour, I want to show you how to start using simple scrolling views in a mini-project.

Implementation Overview

When I say simple, I mean it. This project will consist of a scroll view (UIScrollView) with content added in Interface Builder that extends beyond the physical screen, as shown in Figure 1.

Figure 1. We’re going to make a view. It will scroll.


To enable scrolling in the view, we need to define a property called contentSize, which describes how large the content is that needs to be scrolled. That’s it.

Setting Up the Project

Begin by creating another View-Based Application. Name the new project Scroller. For this example, we’re going to be adding the scroll view (UIScrollView) as a subview to the existing view (UIView) in ScrollerViewController.xib. This is a perfectly acceptable approach, but as you get more experienced with the tools, you might want to just replace the default view entirely.

Preparing the Outlet

There’s only one thing we need to do programmatically in this project, and that’s set a property on the scroll view object. To access the object, we need to create an outlet for it. Open ScrollerViewController.h and add an outlet for a UIScrollView instance called theScroller, and then declare it as a property. The finished header is shown in Listing 1.

Listing 1.
#import <UIKit/UIKit.h>
@interface ScrollerViewController : UIViewController {
IBOutlet UIScrollView *theScroller;
}
@property (nonatomic, retain) UIScrollView *theScroller;
@end

Update the ScrollerViewController implementation file (ScrollerViewController.m) with the corresponding @synthesize directive added after the @implementation directive:

@synthesize theScroller;

Now that we’ll be able to easily access the scroll view, let’s go ahead and add it in Interface Builder.

Adding a Scroll View

Open the ScrollerViewController.xib file in Interface Builder, making sure that the Document window is open (Window, Document) and the view is visible. Using the Object Library (Tools, Library), drag an instance of a scroll view into your view. Position the view however you’d like it to appear and place a label above it that reads Scrolling View (just in case you forget what we’re building).


Adding Objects to the Scroll View

Now that your scroll view is included in the XIB file, you need to populate it with something! Objects are often placed in scroll views by writing code that calculates their position. In Interface Builder, Apple could add the ability to visually position objects in a larger virtual scroll view canvas, but they haven’t.

So, how do we get our buttons and other widgets onscreen? First, start by dragging everything that you want to present into the scroll view object. For this example, I’ve added six labels. You can use buttons, images, or anything else that you’d normally add to a view.

When the objects are in the view, you have two options. First, you can select the object, and then use the arrow keys to position the objects outside of the visible area of the view to “guesstimate” a position. Or second, you can select each object in turn and use the Size Inspector (Command+3) to set their X and Y coordinates manually, as shown in Figure 2.

Figure 2. Use the Size Inspector to set the X and Y point coordinates for each object.


By the Way

The coordinates of objects are relative to the view they are in. In this example, left corner of our scrolling view defines 0,0 (called the “origin point”) for everything we add to it.


To help you out, these are the X,Y coordinates left centers of my six labels:

Label 1 110,45

Label 2 110,125

Label 3 110,205

Label 4 110,290

Label 5 110,375

Label 6 110,460

As you can see from my final view, shown in Figure 3, the sixth label isn’t visible, so we’ll certainly need some scrolling if we’re going to be able to view it!

Figure 3. The final scrolling view, created with labels for content.


Connecting to the Outlet

To connect the scrolling view to theScroller outlet defined earlier, control-drag from the File’s Owner icon in the Document window to the scroll view rectangle. When prompted, choose theScroller as your outlet, as shown in Figure 4.

Figure 4. We’ll need to access the scroll view so we can set its contentSize attribute. Create the connection to the theScroller outlet.

That finishes up our work in Interface Builder. Be sure to save the XIB file, and then switch back into Xcode.

Implementing Scrolling Behavior

For fun, try using Build and Run to run the application as it stands. It will compile and launch, but it doesn’t scroll. In fact, it behaves just like we’d expect a typical nonscrolling view to behave. The reason for this is because we need to tell it the horizontal and vertical sizes of the region it is going to scroll. To do this, we need to set the contentSize attribute to a CGSize value. CGSize is just a simple C data structure that contains a height and a width, and we can easily make one using the CGSizeMake(<width>,<height>) function. For example, to tell our scroll view (theScroller) that it can scroll up to 280 points horizontally and 600 points vertically, we could enter the following:

theScroller.contentSize=CGSizeMake(280.0,600.0);

Guess what? That isn’t just what we could do, it’s what we will do! Edit the ScrollerViewController.m file’s viewDidLoad method to read as follows:

- (void)viewDidLoad {
theScroller.contentSize=CGSizeMake(280.0,600.0);
[super viewDidLoad];
}

Where Did You Get the Width and Height Values?

The width we used in this example is just the width of the scroll view itself. Why? Because we don’t have any reason to scroll horizontally. The height is just a nice number we chose to illustrate that, yes, the view is scrolling. In other words, these are pretty arbitrary! You’ll need to choose them to fit your own content in the way that works best for your application.


Releasing the Object

Edit the dealloc method to release the scroll view, and we’re done:

- (void)dealloc {
[theScroller release];
[super dealloc];
}

Building the Application

The moment of truth has arrived. Does the single line of code make magic? Choose Build and Run, and then try scrolling around the view you created. Everything should work like a charm.

Yes, this was a quick and dirty project, but there seems to be a lack of information on getting started with UIScrollView, and I thought it was important to run through a short tutorial. I hope this gives you new ideas on what you can do to create more feature-rich iPhone interfaces.

Other  
  •  Working with the Windows Phone 7 Application Life Cycle (part 2) - Managing Application State
  •  Working with the Windows Phone 7 Application Life Cycle (part 1) - Observing Application Life Cycle Events
  •  Mobile Application Security : SymbianOS Security - Development and Security Testing
  •  Mobile Application Security : SymbianOS Security - Introduction to the Platform
  •  Java Mobile Edition Security : Permissions and User Controls
  •  Integrating Applications with the Windows Phone OS : Working with Launchers and Choosers
  •  Introducing Windows Phone 7 Launchers and Choosers
  •  Java Mobile Edition Security : Development and Security Testing (part 3) - Code Security & Application Packaging and Distribution
  •  Java Mobile Edition Security : Development and Security Testing (part 2) - Reverse Engineering and Debugging
  •  Java Mobile Edition Security : Development and Security Testing (part 1) - Configuring a Development Environment and Installing New Platforms & Emulator
  •  Java Mobile Edition Security : Configurations, Profiles, and JSRs
  •  Programming the Mobile Web : Performance Optimization
  •  Programming the Mobile Web : Testing and Debugging (part 3) - Client-Side Debugging
  •  Programming the Mobile Web : Testing and Debugging (part 2) - Server-Side Debugging & Markup Debugging
  •  Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs
  •  Windows Phone 7 : Working with Controls and Themes - Adding Transition Effects
  •  Windows Phone 7 : Working with Controls and Themes - Understanding Frame and Page Navigation
  •  Windows Phone 7 : Working with Controls and Themes - Panorama and Pivot Controls
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 5) - Windows Mobile & BlackBerry
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 4) - Windows Mobile & BlackBerry
  •  
    Top 10
    Upgrading to Windows Server 2003 : Planning a Windows NT Domain Upgrade (part 2) - Planning the Active Directory Forest
    iPhone Application Developmen : Using the View-Based Application Template (part 3)
    Server 2008 : Deploying Physical Security
    Working with the windows 7 common file dialogs (part 1)
    Deploying a Native SharePoint 2010 Search Service Application
    Performing Granular Backup Using the SharePoint Central Administration
    Leveraging and Optimizing Search in SharePoint 2010 : Define Content Sources
    Designing a Windows Server 2008 R2 Active Directory : Understanding the Single Domain Model
    jQuery 1.3 : Modifying table appearance (part 4) - Filtering
    IIS 7 Authentication
    Most View
    Leveraging User Account Control in Applications
    Exchange Server 2007 : Administrate Transport Settings - Manage Connectors (Send and Receive)
    Developing Applications for the Cloud on the Microsoft Windows Azure Platform : DNS Names, Certificates, and SSL in the Surveys Application
    SharePoint 2010 : Understanding Windows PowerShell Concepts (part 3)
    Excel Programmer : Change Recorded Code
    SQL Server 2008 : Demystifying Data Types - Computed Columns
    Adobe InDesign CS5 : Importing Graphic Objects (part 3) - Using the Image Import Options Dialog Box
    Introducing Windows Phone 7 and the Windows Phone Platform
    SharePoint 2010 : Understanding Advanced PowerShell Topics
    Mobile Application Security : BlackBerry Security - Development and Security Testing
    Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 2) - Positioning Your Game Sprite on the Screen
    Changes in Windows Vista Affecting SDI
    Wireless Networking in Vista
    iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited
    IIS 7.0 : Setting Up Remote Logging by Using the IIS Manager
    SQL Server 2008 : Working with Multiple-Source Queries - Using Four-Part Database Names & The DTC Explained
    Work with IIS 7.0 : Delegate Rights Assignments
    Programming the Mobile Web : Performance Optimization
    New or Updated Group Policy Settings in Windows Vista
    Designing and Configuring Unified Messaging in Exchange Server 2010 : Unified Messaging Installation (part 1)