SwiftUI设置图像边界

时间:2020-09-28 07:41:10

标签: swift swiftui

我想设置我的右侧图像框边界,如左侧,而不移动图像或文本。我怎样才能做到这一点。我尝试了几种方法,但找不到解决方法。

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该使用内置的TabView。它为您提供了所有功能,并且已经考虑到可访问性。

这里是一个示例(您可以为文本和图像进行更改):

struct ContentView: View {
    
    @State private var selection: Int = 1

    var body: some View {
        TabView(selection: $selection) {
            Text("Tab Content 1")
                .tabItem {
                    Label("1", systemImage: "1.square")
                }
                .tag(1)
            
            Text("Tab Content 2")
                .tabItem {
                    Label("2", systemImage: "2.square")
                }
                .tag(2)
            
            Text("Tab Content 3")
                .tabItem {
                    Label("3", systemImage: "3.square")
                }
                .tag(3)
        }
    }
}

结果:

TabView showing 3 separate sections as an example


自定义版本(高度推荐):

struct ContentView: View {

    var body: some View {
        VStack {
            Spacer()
            
            Text("Main Content")
            
            Spacer()
            
            HStack {
                VStack {
                    Button {
                        //
                    } label: {
                        Label("1", systemImage: "1.square")
                    }
                }.frame(maxWidth: .infinity)
                
                VStack {
                    Button {
                        //
                    } label: {
                        Label("2", systemImage: "2.square")
                    }
                }.frame(maxWidth: .infinity)
                
                VStack {
                    Button {
                        //
                    } label: {
                        Label("3", systemImage: "3.square")
                    }
                }.frame(maxWidth: .infinity)
            }.frame(height: 50)
        }
    }
}