简体   繁体   English

TabView 的 TabItem 的选定选项卡图像在 SwiftUI 中始终为蓝色

[英]Selected tab image for a TabView's TabItem is always blue in SwiftUI

I'm playing around with a TabView in SwiftUI and I'm able to set the image of a tab that is not currently selected, however, setting the image for a selected tab doesn't seem to be working.我正在玩 SwiftUI 中的 TabView,我可以设置当前未选择的选项卡的图像,但是,为选定的选项卡设置图像似乎不起作用。 All I see is a blue circle instead of a black one.我所看到的只是一个蓝色圆圈而不是黑色圆圈。

Here is my code:这是我的代码:

import SwiftUI

struct MainView: View {
    @State private var selected = 0

    var body: some View {

        TabView(selection: $selected){
            Text("First View")
                .tabItem {
                    Image(self.selected == 0 ? "eclipse-tab-icon-black" : "eclipse-tab-icon-grey")
                        .renderingMode(.original)
            }.tag(0)
            Text("Second View")
                .tabItem {
                    Image(systemName: "2.circle")
            }.tag(1)
            Text("Third View")
                .tabItem {
                    Image(systemName: "3.circle")
            }.tag(1)
        }
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()
    }
}

As you can see here, the non-selected tab is grey.正如您在此处看到的,未选择的选项卡是灰色的。 This is correct:这是对的:

在此处输入图像描述

But the selected tab is blue, instead of black:但选定的选项卡是蓝色的,而不是黑色:

推

Assets:资产:

在此处输入图像描述

Asset settings:资产设置:

在此处输入图像描述

If you are supplying your own PNG images for tab views, use rendering mode template so that the default colors are applied to the non-background bits of your images.如果您为选项卡视图提供自己的 PNG 图像,请使用渲染模式template ,以便将默认 colors 应用于图像的非背景位。

If found this easiest to do in the Assets folder.如果在 Assets 文件夹中发现这最容易做到。 Or you can it in code as:或者你可以在代码中:

Image("myImage").renderingMode(.template)

You do not set a selected and not selected icon on your TabView, you only provide one image and it gets rendered in secondary colour when not selected and in accent colour when selected.您没有在 TabView 上设置已选择和未选择的图标,您只提供一个图像,并且在未选择时以辅助颜色呈现,在选择时以强调色呈现。 If you want your selected tab icons to be black you could set the accent on colour on TabView to black, but it is against Apple's design guidelines and you will change the accent colour of all subviews as well.如果您希望您选择的选项卡图标为黑色,您可以将 TabView 上的颜色重音设置为黑色,但这违反了 Apple 的设计准则,您也将更改所有子视图的重音颜色。

Use accentColor of TabView使用 TabView 的 AccentColor

TabView(selection: $selected) {
        SwiftUIMyData()
            .tabItem {
                Image(systemName: "rectangle.fill.on.rectangle.fill")
                Text("My Data")
        }
        .tag(0)
        SwiftUIMyContent()
            .tabItem {
                Image(systemName: "desktopcomputer")
                Text("My Content")
        }
        .tag(1)
        }.accentColor(.black)

Change the rendering mode of your image to original.将图像的渲染模式更改为原始模式。 Either on the xcassets folder, or with the renderingMode modifier in code.在 xcassets 文件夹中,或者在代码中使用 renderingMode 修饰符。

use.accentColor(.black)使用.accentColor(.black)

import SwiftUI

struct ContentView: View {
    @State private var selection = 0

    var body: some View {
        TabView(selection: $selection){
            Text("First View")
                .font(.title)
                .tabItem {
                    VStack {
                        Image("first")
                        Text("First")
                    }
                }
                .tag(0)
            Text("Second View")
                .font(.title)
                .tabItem {
                    VStack {
                        Image("second")
                        Text("Second")
                    }
                }
                .tag(1)
        }.accentColor(.black)
    }
}

tabItem (black) demo tabItem(黑色)演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM