[SwiftUI]TabViewの非選択のタブの色を変えるには?

SwiftUI

どうも、ちょげ(@chogetarou)です。

TabViewの選択されていないタブの色を変える方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

TabViewの非選択のタブの色を変えるには、UITabBar.appearance().unselectedItemTintColorを使います。

具体的には、initメソッドやonAppear修飾子で、UITabBar.appearance().unselectedItemTintColorに色を代入します。

init() {
    //非選択のタブの色を代入
    UITabBar.appearance().unselectedItemTintColor = color
}

UITabBar.appearance().unselectedItemTintColorに代入した色が、TabViewの選択されていないタブの色になります。

使用例

struct ContentView: View {
    
    init() {
        UITabBar.appearance().unselectedItemTintColor = .green.withAlphaComponent(0.3)
    }
    
    var body: some View {
        VStack {
            TabView  {
                PageView(text: "Home View")
                    .tabItem {
                        Image(systemName: "house.fill")
                        Text("home")
                    }
                PageView(text: "Data View")
                    .tabItem {
                        Image(systemName: "folder.fill")
                        Text("Data")
                    }
                PageView(text: "Person View")
                    .tabItem {
                        Image(systemName: "person.fill")
                        Text("Person")
                    }
            }
        }
    }
}

コメント

タイトルとURLをコピーしました