Class

UIScrollView

A view that allows the scrolling and zooming of its contained views.

Overview

UIScrollView is the superclass of several UIKit classes including UITableView and UITextView.

The central notion of a UIScrollView object (or, simply, a scroll view) is that it is a view whose origin is adjustable over the content view. It clips the content to its frame, which generally (but not necessarily) coincides with that of the application’s main window. A scroll view tracks the movements of fingers and adjusts the origin accordingly. The view that is showing its content “through” the scroll view draws that portion of itself based on the new origin, which is pinned to an offset in the content view. The scroll view itself does no drawing except for displaying vertical and horizontal scroll indicators. The scroll view must know the size of the content view so it knows when to stop scrolling; by default, it “bounces” back when scrolling exceeds the bounds of the content.

The object that manages the drawing of content displayed in a scroll view should tile the content’s subviews so that no view exceeds the size of the screen. As users scroll in the scroll view, this object should add and remove subviews as necessary.

Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement. If the timer fires without a significant change in position, the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses, the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.

A scroll view also handles zooming and panning of content. As the user makes a pinch-in or pinch-out gesture, the scroll view adjusts the offset and the scale of the content. When the gesture ends, the object managing the content view should should update subviews of the content as necessary. (Note that the gesture can end and a finger could still be down.) While the gesture is in progress, the scroll view does not send any tracking calls to the subview.

The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.

State Preservation

If you assign a value to this view’s restorationIdentifier property, it attempts to preserve its scrolling-related information between app launches. Specifically, the values of the zoomScale, contentInset, and contentOffset properties are preserved. During restoration, the scroll view restores these values so that the content appears scrolled to the same position as before. For more information about how state preservation and restoration works, see App Programming Guide for iOS.

Topics

Responding to Scroll View Interactions

delegate

The delegate of the scroll-view object.

UIScrollViewDelegate

The methods declared by the UIScrollViewDelegate protocol allow the adopting delegate to respond to messages from the UIScrollView class and thus respond to, and in some affect, operations such as scrolling, zooming, deceleration of scrolled content, and scrolling animations.

Managing the Content Size and Offset

contentSize

The size of the content view.

contentOffset

The point at which the origin of the content view is offset from the origin of the scroll view.

setContentOffset:animated:

Sets the offset from the content view’s origin that corresponds to the receiver’s origin.

Managing the Content Inset Behavior

adjustedContentInset

The insets derived from the content insets and the safe area of the scroll view.

Beta
contentInset

The custom distance that the content view is inset from the safe area or scroll view edges.

contentInsetAdjustmentBehavior

The behavior for determining the adjusted content offsets.

Beta
UIScrollViewContentInsetAdjustmentBehavior

Constants indicating how safe area insets are added to the adjusted content inset.

Beta
adjustedContentInsetDidChange

Called when the adjusted content insets of the scroll view change.

Beta

Getting the Layout Guides

frameLayoutGuide

The layout guide based on the untransformed frame rectangle of the scroll view.

Beta
contentLayoutGuide

The layout guide based on the untranslated content rectangle of the scroll view.

Beta

Configuring the Scroll View

scrollEnabled

A Boolean value that determines whether scrolling is enabled.

directionalLockEnabled

A Boolean value that determines whether scrolling is disabled in a particular direction.

pagingEnabled

A Boolean value that determines whether paging is enabled for the scroll view.

scrollsToTop

A Boolean value that controls whether the scroll-to-top gesture is enabled.

bounces

A Boolean value that controls whether the scroll view bounces past the edge of content and back again.

alwaysBounceVertical

A Boolean value that determines whether bouncing always occurs when vertical scrolling reaches the end of the content.

alwaysBounceHorizontal

A Boolean value that determines whether bouncing always occurs when horizontal scrolling reaches the end of the content view.

Getting the Scrolling State

tracking

Returns whether the user has touched the content to initiate scrolling.

dragging

A Boolean value that indicates whether the user has begun scrolling the content.

decelerating

Returns whether the content is moving in the scroll view after the user lifted their finger.

decelerationRate

A floating-point value that determines the rate of deceleration after the user lifts their finger.

UIScrollViewDecelerationRateNormal

The default deceleration rate for a scroll view.

UIScrollViewDecelerationRateFast

A fast deceleration rate for a scroll view.

Managing the Scroll Indicator and Refresh Control

indicatorStyle

The style of the scroll indicators.

UIScrollViewIndicatorStyle

The style of the scroll indicators. You use these constants to set the value of the indicatorStyle style.

scrollIndicatorInsets

The distance the scroll indicators are inset from the edge of the scroll view.

showsHorizontalScrollIndicator

A Boolean value that controls whether the horizontal scroll indicator is visible.

showsVerticalScrollIndicator

A Boolean value that controls whether the vertical scroll indicator is visible.

flashScrollIndicators

Displays the scroll indicators momentarily.

refreshControl

The refresh control associated with the scroll view.

Scrolling to a Specific Location

scrollRectToVisible:animated:

Scrolls a specific area of the content so that it is visible in the receiver.

Managing Touches

touchesShouldBegin:withEvent:inContentView:

Overridden by subclasses to customize the default behavior when a finger touches down in displayed content.

touchesShouldCancelInContentView:

Returns whether to cancel touches related to the content subview and start dragging.

canCancelContentTouches

A Boolean value that controls whether touches in the content view always lead to tracking.

delaysContentTouches

A Boolean value that determines whether the scroll view delays the handling of touch-down gestures.

directionalPressGestureRecognizer

The underlying gesture recognizer for directional button presses.

Zooming and Panning

panGestureRecognizer

The underlying gesture recognizer for pan gestures.

pinchGestureRecognizer

The underlying gesture recognizer for pinch gestures.

zoomToRect:animated:

Zooms to a specific area of the content so that it is visible in the receiver.

zoomScale

A floating-point value that specifies the current scale factor applied to the scroll view's content.

setZoomScale:animated:

A floating-point value that specifies the current zoom scale.

maximumZoomScale

A floating-point value that specifies the maximum scale factor that can be applied to the scroll view's content.

minimumZoomScale

A floating-point value that specifies the minimum scale factor that can be applied to the scroll view's content.

zoomBouncing

A Boolean value that indicates that zooming has exceeded the scaling limits specified for the receiver.

zooming

A Boolean value that indicates whether the content view is currently zooming in or out.

bouncesZoom

A Boolean value that determines whether the scroll view animates the content scaling when the scaling exceeds the maximum or minimum limits.

Managing the Keyboard

keyboardDismissMode

The manner in which the keyboard is dismissed when a drag begins in the scroll view.

UIScrollViewKeyboardDismissMode

The manner in which the keyboard is dismissed when a drag begins in the scroll view.

Managing the Index

indexDisplayMode

The manner in which the index is shown while the user is scrolling.

UIScrollViewIndexDisplayMode

The manner in which the index is shown while the user is scrolling.

Relationships

Inherits From

Conforms To

See Also

Container Views

Collection Views

Display nested views using a configurable and highly customizable layout.

Table Views

Display data in a single column of customizable rows.

UIStackView

A streamlined interface for laying out a collection of views in either a column or a row.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software