4
1

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 3 years have passed since last update.

[SwiftUI] TabView 対応

Last updated at Posted at 2020-12-28

#SwiftUI でアプリ作成する際のメモです。

TabView (struct、iOS 13.0+) を利用することに。

where SelectionValue : Hashable, Content : View

参照:SwiftUI Structure Tabview reference

tabItem(_:)でタブ付きのユーザーインターフェイスを作成する。

ビューをに配置し、各タブのコンテンツに修飾子を適用します。
以下は、3つのタブを持つタブビューを作成します。

tabview-base.png

basic.swift
TabView {
    Text("The First Tab")
        .tabItem {
            Image(systemName: "1.square.fill")
            Text("First")
        }
    Text("Another Tab")
        .tabItem {
            Image(systemName: "2.square.fill")
            Text("Second")
        }
    Text("The Last Tab")
        .tabItem {
            Image(systemName: "3.square.fill")
            Text("Third")
        }
}
.font(.headline)

PageTabViewStyleでページングスクロールビューを作成する

[[参照:SwiftUI tabviewstyle]
(https://developer.apple.com/documentation/swiftui/tabviewstyle)

tabview-paging.png

CustomTabView.swift
    ...省略
    @State var selection = 0
    var body: some View {
        TabView(selection: $selection) {
                    ForEach(0..<contentList.count) { index in
                        HStack {
                            Spacer()
                            Text(contentList[index].systemImage)
                                .padding()
                            Image.init(systemName: contentList[index].systemImage)
                            Spacer()
                        }
                    }
                }
                .tabViewStyle(PageTabViewStyle())
    ...省略
    }

TabView + ZStack + Button + State でページングをボタンで制御する

tabview-paging-button.png

ボタンでTabViewを制御するため、tag を付けること
func tag<V>(_ tag: V) -> some View where V : Hashable

CustomTabView.swift
        TabView(selection: $selection) {
               ForEach(0..<contentList.count) { index in
                      //表示内容
                      .tag(index)
               }
         }
         .tabViewStyle(PageTabViewStyle())

参照 GitHub - CustomTabView.swift

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?