Scroll View Programming
1. The UIScrollView class contains no specially defined view for the content that it displays; instead it simply scrolls its subviews.
2. Adding basic pinch-in and pinch-out zoom support requires that the scroll view use delegation.
Creating and Configuring Scroll Views
1. There are only two additional steps required to complete the scroll view configuration:(1)You must set the contentSize property to the size of the scrollable content.
(2)You must also add a view or views that are displayed and scrolled by the scroll view.
(3)You can optionally configure any visual cues your application requires—vertical and horizontal scroll indicators, drag bouncing, zoom bouncing, and directional constraint of scrolling
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(1280,960);
}
Creating Scroll Views Programmatically
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,758);
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
}
Configuring The Scroll View Content Size, Content Inset, And Scroll Indicators


1. To add padding to your application must set the contentInset property of the scroll view. The contentInset property specifies a buffer area around the content of the scroll view. The contentInset property is a UIEdgeInsets
struct with the fields top, bottom, left, right.
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
self.view=scrollView;
scrollView.contentSize=CGSizeMake(320,758);
scrollView.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
}
2. However, changing the contentInset value has an unexpected side effect when your scroll view displays scroll indicators. To correct this, you must set the scrollIndicatorInsets property.
- (void)loadView {
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
scrollView.contentSize=CGSizeMake(320,758);
scrollView.contentInset=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
scrollView.scrollIndicatorInsets=UIEdgeInsetsMake(64.0,0.0,44.0,0.0);
// do any further configuration to the scroll view
// add a view, or views, as a subview of the scroll view.
// release scrollView as self.view retains it
self.view=scrollView;
[scrollView release];
Scrolling the Scroll View Content
Scrolling Programmatically
1. Scrolling to a Specific Offset
Scrolling to a specific top-left location (the contentOffset property) can be accomplished in two ways.
Scrolling to a specific top-left location (the contentOffset property) can be accomplished in two ways.
(1)The setContentOffset:animated: method scrolls the content to the specified content offset. The delegate is sent a scrollViewDidScroll: message.
(2) If animation is disabled, or if you set the content offset by setting the contentOffset property directly, the delegate receives a single scrollViewDidScroll: message. If animation is enabled, then the delegate receives
a series of scrollViewDidScroll: messages as the animation is in progress. When the animation is complete, the delegate receives a scrollViewDidEndScrollingAnimation: message.
2. Making a rectangle visible
The scrollRectToVisible:animated: method scrolls the specified rectangle so that it is just visible inside the scroll view. If the animated parameter is YES, the rectangle is scrolled into view at a constant pace.
The scrollRectToVisible:animated: method scrolls the specified rectangle so that it is just visible inside the scroll view. If the animated parameter is YES, the rectangle is scrolled into view at a constant pace.
3. Scroll To Top
If the status bar is visible a scroll view can also scroll to the top of the content in response to a tap on the status bar. This practice is common in applications that provide a vertical representation of data. .
If the status bar is visible a scroll view can also scroll to the top of the content in response to a tap on the status bar. This practice is common in applications that provide a vertical representation of data. .
Your application enables this behavior by implementing the delegate method the scroll view property scrollViewShouldScrollToTop: and return YES.

Delegate Messages Sent During Scrolling
As scrolling occurs, the scroll view tracks state using the tracking, dragging, decelerating, and zooming properties. In addition, the contentOffset property defines the point in the content that is visible at the top left of the scroll view’s bounds.. The following table describes each of the state properties:The Simple Approach: Tracking The Start and Completion Of A Scroll Action
(1) Implement the scrollViewWillBeginDragging: method to receive notification that dragging will begin.
(2) To determine when scrolling is complete you must implement two delegate methods: scrollViewDidEndDragging:willDecelerate: andscrollViewDidEndDecelerating:.
Scrolling is completed either when the delegate receives the scrollViewDidEndDragging:willDecelerate: message with NO as the decelerate parameter, or when your delegate receives the scrollViewDidEndDecelerating: method. In
either case, scrolling is complete.
The Complete Delegate Message Sequence