はじめに
SwiftUIはiOS17からページングができるようになります。
しかし、iOS16以下でもページングは使用したいです。
SwiftUIIntrospect
を使用すれば実現できます。
サンプルアプリ
実装
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
は必須ライブラリです