5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftUI Listのグループ化スタイル iOS14対応

5
Last updated at Posted at 2020-09-30

概要

iOS13、iOS14でグループ化リストを表示する際のTipsです。

SwiftUIでグループ化されたListを表示

iOS13の**GroupedListStyle()と、iOS14で追加されたInsetGroupedListStyle()**を切り分けて設定していきます。

iOS13

SwiftUIでグループ化されたテーブルを表示する場合、iOS13では

List {
    Text("List Item 1")
    Text("List Item 2")
    Text("List Item 3")
}.listStyle(GroupedListStyle())

としていました。

iOS14

iOS14では、**InsetGroupedListStyle()**を使用して

List {
    Text("List Item 1")
    Text("List Item 2")
    Text("List Item 3")
}
.listStyle(InsetGroupedListStyle())

とします。

iOS13/iOS14 両方対応

使い易いようにViewを拡張して

extension View {
    func iOS14Coordinator_ListStyle() -> some View {
        Group{
            if #available(iOS 14, *) {
                self
                    .listStyle(InsetGroupedListStyle())
            }else{
                self
                    .listStyle(GroupedListStyle())
            }
      }
    }
}

以下のように使います。

List {
    Text("List Item 1")
    Text("List Item 2")
    Text("List Item 3")
}
.iOS14Coordinator_ListStyle()

https://developer.apple.com/documentation/swiftui/insetgroupedliststyle
https://developer.apple.com/documentation/swiftui/groupedliststyle

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?