Class

UIScroll​View

Supports the display of content that does not fit entirely on the screen and enables users to scroll and zoom the content.

Overview

UIScroll​View is the superclass of several UIKit classes including UITable​View and UIText​View.

The central notion of a UIScroll​View 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 touches​Should​Begin(_:​with:​in:​), is​Paging​Enabled, and touches​Should​Cancel(in:​) 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 UIScroll​View class can have a delegate that must adopt the UIScroll​View​Delegate protocol. For zooming and panning to work, the delegate must implement both view​For​Zooming(in:​) and scroll​View​Did​End​Zooming(_:​with:​at​Scale:​); in addition, the maximum (maximum​Zoom​Scale) and minimum ( minimum​Zoom​Scale) zoom scale must be different.

For information about basic view behaviors, see View Programming Guide for iOS.

State Preservation

If you assign a value to this view’s restoration​Identifier property, it attempts to preserve its scrolling-related information between app launches. Specifically, the values of the zoom​Scale, content​Inset, and content​Offset 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.

Symbols

Managing the Display of Content

func set​Content​Offset(CGPoint, animated:​ Bool)

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

var content​Offset:​ CGPoint

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

var content​Size:​ CGSize

The size of the content view.

var content​Inset:​ UIEdge​Insets

The distance that the content view is inset from the enclosing scroll view.

Managing Scrolling

var is​Scroll​Enabled:​ Bool

A Boolean value that determines whether scrolling is enabled.

var is​Directional​Lock​Enabled:​ Bool

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

var scrolls​To​Top:​ Bool

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

func scroll​Rect​To​Visible(CGRect, animated:​ Bool)

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

var is​Paging​Enabled:​ Bool

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

var bounces:​ Bool

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

var always​Bounce​Vertical:​ Bool

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

var always​Bounce​Horizontal:​ Bool

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

func touches​Should​Begin(Set<UITouch>, with:​ UIEvent?, in:​ UIView)

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

func touches​Should​Cancel(in:​ UIView)

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

var can​Cancel​Content​Touches:​ Bool

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

var delays​Content​Touches:​ Bool

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

var deceleration​Rate:​ CGFloat

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

var is​Dragging:​ Bool

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

var is​Tracking:​ Bool

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

var is​Decelerating:​ Bool

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

var directional​Press​Gesture​Recognizer:​ UIGesture​Recognizer

The underlying gesture recognizer for directional button presses.

Managing the Scroll Indicator and Refresh Control

var scroll​Indicator​Insets:​ UIEdge​Insets

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

var shows​Horizontal​Scroll​Indicator:​ Bool

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

var shows​Vertical​Scroll​Indicator:​ Bool

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

func flash​Scroll​Indicators()

Displays the scroll indicators momentarily.

var refresh​Control:​ UIRefresh​Control?

The refresh control associated with the scroll view.

Zooming and Panning

var pan​Gesture​Recognizer:​ UIPan​Gesture​Recognizer

The underlying gesture recognizer for pan gestures.

var pinch​Gesture​Recognizer:​ UIPinch​Gesture​Recognizer?

The underlying gesture recognizer for pinch gestures.

func zoom(to:​ CGRect, animated:​ Bool)

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

var zoom​Scale:​ CGFloat

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

func set​Zoom​Scale(CGFloat, animated:​ Bool)

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

var maximum​Zoom​Scale:​ CGFloat

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

var minimum​Zoom​Scale:​ CGFloat

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

var is​Zoom​Bouncing:​ Bool

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

var is​Zooming:​ Bool

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

var bounces​Zoom:​ Bool

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

Managing the Delegate

var delegate:​ UIScroll​View​Delegate?

The delegate of the scroll-view object.

Managing the Keyboard

var keyboard​Dismiss​Mode:​ UIScroll​View​Keyboard​Dismiss​Mode

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

Managing the Index

var index​Display​Mode:​ UIScroll​View​Index​Display​Mode

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

Beta

Constants

UIScroll​View​Indicator​Style

The style of the scroll indicators. You use these constants to set the value of the indicator​Style style.

Deceleration Constants

The rate of deceleration for a scrolling view.

UIScroll​View​Keyboard​Dismiss​Mode

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

UIScroll​View​Index​Display​Mode

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

Beta

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