はじめに
Listのセパレーターは微妙に左側にスペースがあるんですよね
このスペースを消したいです。
しかし、SwiftUIの標準機能ではないのでSwiftUI-Introspect
を使って実装します
ライブラリのインストール
全体的な流れはこちらを参考にしてください
実装
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
めっちゃすき