LoginSignup
1
2

More than 1 year has passed since last update.

【SwiftUI】Listをカスタマイズ

Last updated at Posted at 2022-07-03

はじめに

SwiftUIでTableViewと同じような見た目を作成するにはListを使用する必要があります。
しかし、Listでは変更できない項目が多く存在します。
おそらくListはUITableViewで作成されているので、UITableViewの設定が反映されます。

それを使用して、公式のAPIでは提供されていない機能を実装します

背景色を変更

import SwiftUI

struct ContentView: View {
    init() {
        UITableView.appearance().backgroundColor = .red
    }
    var body: some View {
        List {
            ForEach(0..<10) { _ in
                Text("テキスト")
            }
        }
        .listStyle(.grouped)
    }
}

区切り線の色を変更

import SwiftUI

struct ContentView: View {
    init() {
        UITableView.appearance().separatorColor = .red
    }
    var body: some View {
        List {
            ForEach(0..<10) { _ in
                Text("テキスト")
            }
        }
        .listStyle(.grouped)
    }
}

区切り線の長さを変更

import SwiftUI

struct ContentView: View {
    init() {
        UITableView.appearance().separatorInset = .init(top: 0, left: 0, bottom: 0, right: 20)
    }
    var body: some View {
        List {
            ForEach(0..<10) { _ in
                Text("テキスト")
            }
        }
        .listStyle(.grouped)
    }
}

バウンスの無効化

import SwiftUI

struct ContentView: View {
    init() {
        UIScrollView.appearance().bounces = false
    }
    var body: some View {
        List {
            ForEach(0..<10) { _ in
                Text("テキスト")
            }
        }
        .listStyle(.grouped)
    }
}

おわり

まだあると思うので気づいたら追加します。
不便なので早く公式APIとして提供してほしいです。

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