LoginSignup
0
0

More than 3 years have passed since last update.

iOS13とiOS14で似たように振る舞うTableView

Last updated at Posted at 2021-02-25

SwiftUIではUITableViewのような振る舞いを持つViewにListが存在します。
このListはなかなか曲者でiOS13とiOS14ではデザインの設定方法が異なり、なかなか思うように扱うことが出来ません。

iOS14からは遅延読み込みが可能なLazyVStackが登場しているのでこちらを代替手段として用いるのが簡単です。

ということで、OSのバージョンでListとLazyVStackの出し分けをしつつ似たような見た目の振る舞いを持つViewを作成してみます。

struct TableView<Content: View>: View {
    let content: () -> Content

    let rowBackgroundColor: Color

    init(rowBackgroundColor: Color, _ content: @escaping () -> Content) {
        self.content = content
        self.rowBackgroundColor = rowBackgroundColor

        UITableView.appearance().separatorStyle = .none // この区切り線を消すコードはiOS13でのみ有効
        UITableView.appearance().backgroundColor = .clear
    }

    var body: some View {
        if #available(iOS 14, *) {
            ScrollView {
                LazyVStack(spacing: 0) {
                    self.content()
                        .background(self.rowBackgroundColor)
                }
            }
        } else {
            List {
                self.content()
                    .listRowInsets(EdgeInsets())
                    .listRowBackground(self.rowBackgroundColor)
            }
            .environment(\.defaultMinListRowHeight, 0)
        }
    }
}

このViewは以下のように利用することで、似たような振る舞いをもたせることが可能です。

struct ContentView: View {
    var body: some View {
        TableView(rowBackgroundColor: .blue) {
            ForEach(0..<100) { index in
                Group {
                    HStack {
                        Circle()
                            .frame(width: 50, height: 50)
                        Text("No, \(index)")
                        Spacer()
                    }
                    .padding(.vertical, 12)
                    .padding(.horizontal, 20)
                }
            }
        }
        .background(Color.blue.edgesIgnoringSafeArea(.all))
    }
}
0
0
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
0
0