1
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 3 years have passed since last update.

SwiftUIのCollectionView! LazyGridで画像を並べてみる

Posted at

SwiftUIのLazyGridをシンプルにつかう方法です

SwiftUIでCollectionViewってどうやるの?

マス目にコンテンツが並んだCollectionView。
SwiftUIでどうやればできるのでしょうか。

LazyGrid

LazyGridで、SwiftUIのレイアウト機能を活かしつつ、CollectionViewが作成できます。

方法

struct ContentView: View {
    var columns: [GridItem] =
    Array(repeating: .init(.flexible()), count: 2)
    var images: [String] = ["image0","image1","image2","image3"...]
    
    var body: some View {
        ScrollView(.vertical) {
            LazyVGrid(columns: columns, alignment: .center, spacing: 10) {
                ForEach(images, id: \.self) {imageName in
                    Image(imageName)
                        .resizable()
                        .aspectRatio(1, contentMode: .fill)
                }
            }
        }
    }
}

ScrollViewに入れているのでスクロールします。

🐣


フリーランスエンジニアです。
お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLやARKitを使ったアプリを作っています。
機械学習/AR関連の情報を発信しています。

Twitter
Medium

1
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
1
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?