はじめに
iOS17からSectionにisExpandedという引数が追加されました。
これを使用することで、Sectionを開閉することができるようになります。
サンプルアプリ
実装
ヘッダーの横に矢印ボタンが出るのはListStyleのsidebarだけでした。
その他のスタイルではisExpanded.toggle()
のように値を直接変更しないと開閉できません。
import SwiftUI
struct ContentView: View {
@State var isExpanded = false
var body: some View {
List {
Section(isExpanded: $isExpanded) {
Text("コンテンツ")
} header: {
Text("ヘッダー")
}
Button {
withAnimation {
isExpanded.toggle()
}
} label: {
Text("開閉")
}
}
.listStyle(.sidebar)
}
}
おわり
自前で実装するのも簡単そうですが、公式で提供してくれるのは嬉しいですね
公式ドキュメント