SwiftUI Academy · レッスン

ScrollView Readerとアンカー

特定の項目までプログラムでスクロールします。

レッスン 4/413 ステップ

「ScrollView Readerとアンカー」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Jumping to Content

Sometimes you need to scroll to a specific item in code, like a Back to Top button. ScrollViewReader makes that possible. 🎯

Wrap the ScrollView

Place a ScrollViewReader around your ScrollView; it hands you a proxy you use to control scrolling.

ScrollViewReader { proxy in
    ScrollView {
        Content()
    }
}

Tag Each Item

Give scrollable items a unique id so the reader knows exactly where to jump when you ask it to.

ForEach(items) { item in
    Row(item).id(item.id)
}

Call scrollTo

The proxy has one key method: scrollTo. Pass an id and the ScrollView glides that item into view.

proxy.scrollTo(42)

Animate the Jump

Wrap the call in withAnimation so the scroll glides smoothly instead of snapping instantly to the target.

withAnimation {
    proxy.scrollTo(42)
}

Anchor the Position

The anchor argument decides where the item lands: .top pins it to the top, .center to the middle.

proxy.scrollTo(42, anchor: .top)

Back to Top Button

A common pattern is a button that calls scrollTo on the first item id to whisk the user back up.

Button("Top") {
    proxy.scrollTo(firstID, anchor: .top)
}

Jump on Appear

Call scrollTo inside onAppear to open a long list already scrolled to the user latest unread row.

.onAppear {
    proxy.scrollTo(lastReadID)
}

IDs Must Match

If scrollTo does nothing, check that the id you pass exactly matches an id set on a visible item.

Pair with State

Trigger scrolling from a @State change, like a search result selection, to guide the user automatically.

onChange(of: selected) { id in
    proxy.scrollTo(id)
}

Effortless Navigation

With ScrollViewReader and anchors, long screens feel navigable and friendly instead of an endless swipe. ✨

Quick Check

Think about how you move to a specific row.

Recap

You learned to use ScrollViewReader, tag items with ids, and call scrollTo with anchors to jump anywhere in code. 🎉

無料で開始

AI チューターと学ぶ Swift — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「ScrollView Readerとアンカー」レッスンは無料ですか?

はい。「ScrollView Readerとアンカー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。

「ScrollView Readerとアンカー」で何を学びますか?

特定の項目までプログラムでスクロールします。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

SwiftUI Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「ScrollView Readerとアンカー」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSwiftUI Academyレッスンでコードを書いて実行できますか?

はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 横方向と縦方向のScrollView
  2. LazyVStackとLazyHStack
  3. LazyVGridで適応型グリッドを作る
  4. ScrollView Readerとアンカー
← SwiftUI Academyに戻る