Skip to main content

How to make a ScrollView start at the bottom

About 2 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to make a ScrollView start at the bottom 관련

SwiftUI by Example

Back to Home

How to make a ScrollView start at the bottom | SwiftUI by Example

How to make a ScrollView start at the bottom

Updated for Xcode 15

New in iOS 17

SwiftUI's ScrollView starts scrolling from the top by default, but if you want to create a UI like Apple's Messages app you can ask the scroll view to start at the bottom by using the scrollPosition() modifier with an initial anchor of .bottom.

For example, this shows 50 text views in a scroll view, but asks the scroll view to start its position from the bottom rather than the top:

ScrollView {
    ForEach(0..<50) { i in
        Text("Item \(i)")
            .frame(maxWidth: .infinity)
            .padding()
            .background(.blue)
            .clipShape(.rect(cornerRadius: 25))
    }
}
.scrollPosition(initialAnchor: .bottom)

Download this as an Xcode projectopen in new window

A scrollview containing many items, showing that it scrolls from the bottom up rather than the top down.
A scrollview containing many items, showing that it scrolls from the bottom up rather than the top down.

If your UI alters somehow without the user scrolling – for example if the keyboard appears, or you adjust the size of the scroll view – then the scroll position will remain anchored to the bottom. However, if the user adjusts the scroll position manually, it will scroll freely as normal.

Tips

The initialAnchor parameter is any UnitPoint, so you can use .trailing to start a horizontal scroll view from the right edge, or any exact value you need for your UI.

Similar solutions…
How to display a bottom sheet | SwiftUI by Example

How to display a bottom sheet
How to make a ScrollView snap with paging or between child views | SwiftUI by Example

How to make a ScrollView snap with paging or between child views
How to follow this quick start guide | SwiftUI by Example

How to follow this quick start guide
How to start an animation immediately after a view appears | SwiftUI by Example

How to start an animation immediately after a view appears
How to flash the scroll bar indicators of a ScrollView or List | SwiftUI by Example

How to flash the scroll bar indicators of a ScrollView or List

이찬희 (MarkiiimarK)
Never Stop Learning.