1
2

More than 1 year has passed since last update.

【SwiftUI】特定の条件下でTabを非表示にする

Posted at

はじめに

SwiftUI-Introspectを使ってSwiftUIではできないことをサポートしてもらいます。

ライブラリのインストール

全体的な流れはこちらを参考にしてください

注意
SwiftUIIntrospectのみチェックをいれます
スクリーンショット 2023-08-01 16.18.26.png

サンプルアプリ

Simulator Screen Recording - iPhone 14 Pro - 2023-08-01 at 16.20.46.gif

実装

import SwiftUI
import SwiftUIIntrospect

struct ContentView: View {
    @State private var isHidden = false
    
    var body: some View {
        TabView {
            Button {
                isHidden.toggle()
            } label: {
                Text("Tab1")
            }
            .tabItem {
                Label("1", systemImage: "1.circle.fill")
            }
            
            Button {
                isHidden.toggle()
            } label: {
                Text("Tab2")
            }
            .tabItem {
                Label("2", systemImage: "2.circle.fill")
            }
            
            Button {
                isHidden.toggle()
            } label: {
                Text("Tab3")
            }
            .tabItem {
                Label("3", systemImage: "3.circle.fill")
            }
        }
        .introspect(.tabView, on: .iOS(.v13, .v14, .v15, .v16, .v17)) { tabView in
            tabView.tabBar.isHidden = isHidden
        }
    }
}

おわり

SwiftUIを使う時にSwiftUI-Introspectは必須ですね

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