ScrollView

A scrollable view.

ComponentiOS 13.0+macOS 10.15+tvOS 13.0+watchOS 6.0+ScrollView in SwiftUI

Declaration

React
import { ScrollView } from 'swiftui-react-native';
Swift
nonisolated struct ScrollView<Content> where Content : View

Initializers

One SwiftUI initializer backs <ScrollView>.

React
<ScrollView
  axes?: SwiftUI.AxisSet
>
  children?: ReactNode
</ScrollView>
Swift
@export(implementation) nonisolated init(
    _ axes: Axis.Set = .vertical,
    @ContentBuilder content: () -> Content
)
  • The scroll view's scrollable axis. The default axis is the vertical axis.

    Default `.vertical`

  • childrenReactNode

    Content. Strings become Text; React Native views are laid out by Yoga and placed by SwiftUI.

    Swift content: () -> Content

Also takes the common view props: modifiers, style and testID.

Overview

The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.

In the following example, a ScrollView allows the user to scroll through a VStack containing 100 Text views. The image after the listing shows the scroll view's temporarily visible scrollbar at the right; you can disable it with the showsIndicators parameter of the ScrollView initializer.

var body: some View {
    ScrollView {
        VStack(alignment: .leading) {
            ForEach(0..<100) {
                Text("Row \($0)")
            }
        }
    }
}

Controlling Scroll Position

You can influence where a scroll view is initially scrolled by using the View.defaultScrollAnchor(_:) view modifier.

Provide a value of UnitPoint.center to have the scroll view start in the center of its content when a scroll view is scrollable in both axes.

ScrollView([.horizontal, .vertical]) {
    // initially centered content
}
.defaultScrollAnchor(.center)

Or provide an alignment of UnitPoint.bottom to have the scroll view start at the bottom of its content when a scroll view is scrollable in its vertical axes.

ScrollView {
    // initially bottom aligned content
}
.defaultScrollAnchor(.bottom)

After the scroll view initially renders, the user may scroll the content of the scroll view.

To perform programmatic scrolling, wrap one or more scroll views with a ScrollViewReader.

From Apple’s SwiftUI documentation. Code samples there are Swift.

Relationships

Conforms to

  • View

See also

More in Layout.