Skip to main content

SwiftUIのListの痒いところを調べてみた

SwiftUIを学べる本

ぼちぼちSwiftUIをやってるマスタカです
今回Listを使うために諸々調べたのでそのまとめ


・ListViewの高さの最小値の変更
https://stackoverflow.com/questions/58242342/swiftui-row-height-of-list-how-to-control

List {
    
}
.environment(\.defaultMinListRowHeight, 0)

 

・List内の要素のpaddingの削除
https://stackoverflow.com/questions/68043037/how-can-i-remove-the-minimum-vertical-padding-or-minimum-height-of-a-list-row-in

List{
    HStack {
        Text(name)
    }
    .listRowInsets(EdgeInsets())
}

 

・ForEachに数字のラベルをつける
\.offsetをidに指定する必要がある
これつけないとindexが増えない
https://stackoverflow.com/questions/57244713/get-index-in-foreach-in-swiftui

\は以下の意味があるとのこと
>In SwiftUI, the backslash operator is used to refer keypath to use inside given block.
https://stackoverflow.com/questions/56489766/what-is-the-backslash-used-for-in-swiftui

ForEach(Array(array.enumerated()), id: \.offset) { index, element in
}

 

・Listの各要素タップのハイライト
Buttonにする必要があるとのこと。
これ実装微妙に思うんだけど・・・
https://note.com/taatn0te/n/n225eb65839bc

 

・Scrollで特定の位置に移動する
https://capibara1969.com/3562/

ScrollViewReader { scrollProxy in
    ForEach(Array(array.enumerated()), id: \.offset) { index, element in
        Text().id(index)
    }
    Button(action: {
        //0で一番上に行く
        scrollProxy.scrollTo(0)
    }){
    }
}

 

SwiftUIを学べる本

関連記事:

Pocket