简体   繁体   English

TabView SwiftUI 中 TabItem 上的 LinearGradient

[英]LinearGradient on TabItem in TabView SwiftUI

Is it possible to apply a LinearGradient to a TabItem?是否可以将 LinearGradient 应用于 TabItem? AccentColor only takes a color, and I haven't found another way to apply any color to a TabItem. AccentColor 只需要一种颜色,我还没有找到另一种将任何颜色应用于 TabItem 的方法。 Any Ideas?有任何想法吗?例如,这是从蓝色到绿色的渐变(This in a Gradient from blue to green, for example) (例如,这是从蓝色到绿色的渐变)

There is no chance to apply gradient in SwiftUI for the accentColor.没有机会在 SwiftUI 中应用渐变色作为强调色。 So in your case you will have to create your own Tab Bar Items Images with a gradient and save them as a png.因此,在您的情况下,您将必须使用渐变创建自己的标签栏项目图像并将它们保存为 png。 You will have to match the sizes, as you can not resize Image inside tabItem.您必须匹配大小,因为您无法调整Image内的图像大小。 See this answer for the sizes有关尺寸,请参见此答案

You will need an active Image with your gradient and a inactive Image maybe gray like the default TabBar behavior.您将需要一个带有渐变的活动图像和一个可能是灰色的非活动图像,就像默认的 TabBar 行为一样。 Then just load your Images in the TabView and depending on the selection..然后只需在 TabView 中加载您的图像并根据选择..

Here is an example这是一个例子

struct ContentView: View {
    @State var selection = 0
    var body: some View {
        TabView(selection: $selection) {
            Text("First")
                .tabItem {
                    Image(selection == 0 ? "home_active" : "home_inactive") //<< here you load your different images 

                    Text("Home")
                        .colorMultiply(Color.yellow)
                }

            Text("Second")
                .tabItem {
                    Image(selection == 1 ? "setting_active" : "setting_inactive")
                    Text("Setting")
                }
        }
    }
}

在此处输入图像描述

Yes we cannot apply gradient on tabItem in SwiftUI.是的,我们不能在 SwiftUI 中的 tabItem 上应用渐变。 It only shows no more than one Image and one Text view, although we have put more views inside.tabItem{}.它只显示不超过一个 Image 和一个 Text 视图,尽管我们在 .tabItem{} 中放置了更多视图。

So that you have to make custom tab bar.这样您就必须制作自定义标签栏。

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

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