3
2

More than 1 year has passed since last update.

【SwiftUI】Listのセパレーターを両端まで伸ばす

Posted at

はじめに

Listのセパレーターは微妙に左側にスペースがあるんですよね
このスペースを消したいです。
simulator_screenshot_B16016A9-4155-4875-BD2A-AA13B73A014F.png

しかし、SwiftUIの標準機能ではないのでSwiftUI-Introspectを使って実装します

ライブラリのインストール

全体的な流れはこちらを参考にしてください

注意
SwiftUIIntrospectのみチェックをいれます
スクリーンショット 2023-08-01 16.18.26.png

実装

import SwiftUI
import SwiftUIIntrospect

struct ContentView: View {
    var body: some View {
        List {
            ForEach(0..<50) { _ in
                Text("サンプルセル")
            }
        }
        .listStyle(.plain)
        .introspect(.list(style: .plain), on: .iOS(.v13, .v14, .v15)) { list in
            list.separatorInset = .zero
        }
        .introspect(.list(style: .plain), on: .iOS(.v16, .v17)) { list in
            var listConfig = UICollectionLayoutListConfiguration(appearance: .plain)
            listConfig.separatorConfiguration.topSeparatorInsets = .zero
            listConfig.separatorConfiguration.bottomSeparatorInsets = .zero
            let layout = UICollectionViewCompositionalLayout.list(using: listConfig)
            list.collectionViewLayout = layout
        }
    }
}

おわり

SwiftUI-Introspectめっちゃすき

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