3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】ScrollViewをページングできるようにする

Posted at

はじめに

SwiftUIはiOS17からページングができるようになります。

しかし、iOS16以下でもページングは使用したいです。
SwiftUIIntrospectを使用すれば実現できます。

サンプルアプリ

Simulator Screen Recording - iPhone 14 Pro - 2023-08-10 at 15.27.33.gif

実装

import SwiftUI
import SwiftUIIntrospect

struct ContentView: View {
    @State private var isLoading = false
    var body: some View {
        GeometryReader { proxy in
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 0) {
                    ForEach(0..<50) { _ in
                        RoundedRectangle(cornerRadius: 10)
                            .frame(width: proxy.size.width)
                            .frame(maxHeight: .infinity)
                            .foregroundStyle(Color(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1)))
                    }
                }
            }
            .introspect(.scrollView, on: .iOS(.v13, .v14, .v15, .v16, .v17)) { scrollView in
                scrollView.isPagingEnabled = true
            }
        }
    }
}

おわり

SwiftUIを使うならSwiftUIIntrospectは必須ライブラリです

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?