Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SwiftUI的选项卡视图中显示所选选项卡_Swiftui_Tabview - Fatal编程技术网

在SwiftUI的选项卡视图中显示所选选项卡

在SwiftUI的选项卡视图中显示所选选项卡,swiftui,tabview,Swiftui,Tabview,在SwiftUI中使用TabView时,如何显示所选选项卡,如下图所示 我尝试在每个选项卡中创建一个VStack,如下所示: struct ContentView: View { @State public var tabViewSelection = 0 var body: some View { TabView(selection: $tabViewSelection) { HomeFirstLevel() .tabItem {

在SwiftUI中使用TabView时,如何显示所选选项卡,如下图所示

我尝试在每个选项卡中创建一个VStack,如下所示:

struct ContentView: View {
@State public var tabViewSelection = 0
var body: some View {
    TabView(selection: $tabViewSelection) {
        HomeFirstLevel()
            .tabItem {
                VStack {
                    Image("HomeIcon")
                    Rectangle()
                        .frame(height: 7)
                        .foregroundColor((tabViewSelection == 0) ? .black : .clear)
                }
            }.tag(0)
        }
    }
}
但它不起作用。 我甚至无法添加矩形而不是图像:

HomeFirstLevel()
   .tabItem {
       Rectangle()
   }.tag(0)
TabView不接受形状吗?
提前感谢您的帮助

不能在tabItem中设置形状。但是您可以使用
ZStack
在选项卡栏上添加形状并设置x位置

这是演示

struct ContentViewTabDemo: View {
    @State public var tabViewSelection = 0
    
    private var singleTabWidth = UIScreen.main.bounds.width / 5
    
    var body: some View {
        ZStack(alignment: .bottomLeading) {
            TabView(selection: $tabViewSelection) {
                Color.red
                    .tabItem {
                        VStack {
                            Image(systemName: "circle.fill")
                        }
                    }.tag(0)
                
                Color.blue
                    .tabItem {
                        VStack {
                            Image(systemName: "heart.fill")
                        }
                    }.tag(1)
                
                Color.red
                    .tabItem {
                        VStack {
                            Image(systemName: "circle.fill")
                        }
                    }.tag(2)
                
                Color.blue
                    .tabItem {
                        VStack {
                            Image(systemName: "heart.fill")
                        }
                    }.tag(3)
                
                Color.red
                    .tabItem {
                        VStack {
                            Image(systemName: "circle.fill")
                        }
                    }.tag(4)
            }
            
            Rectangle()
                .offset(x: singleTabWidth * CGFloat(tabViewSelection))
                .frame(width: singleTabWidth, height: 7)
                .padding(.bottom, 2)
                .animation(.default)
        }
    }
}