繁体   English   中英

如何在工具栏项目中添加空间(SwiftUI)

[英]How to add space in toolbar items (SwiftUI)

Spacer() 在 ToolbarItem 中没有效果,我想让项目分散而不是聚集。 你知道如何解决这个问题吗? 谢谢你。

这是代码。

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            VStack{
                Image("searchbar")
                Text("Test")
            }
            .toolbar {
                ToolbarItem(placement: .principal) {
                    HStack {
                        Text("9scoretrain")
                        Spacer()
                        NavigationLink(
                            destination: Text("SearchView"),
                            label: {
                                Image("searchbar")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width:200)
                            })
                        Spacer()
                        NavigationLink(
                            destination: Text("CameraView"),
                            label: {
                                Image(systemName: "camera.viewfinder")
                            })
                    }
                }
            }
        }
    }
}

这是目前的效果。 在此处输入图像描述

这是我想要的项目均匀分布的效果。 在此处输入图像描述

此方法可能会解决,但居中项目和尾随项目之间仍有一些空间。

这仍然不是我想要的,我希望物品均匀分布。

这就是效果。 在此处输入图像描述

这是代码。

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            VStack{
                Image("searchbar")
                Text("Test")
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarLeading) {
                    Text("9scoretrain")
                }
                
                ToolbarItem(placement: .principal) {
                    NavigationLink(
                        destination: Text("SearchView"),
                        label: {
                            Image("searchbar")
                                .resizable()
                                .aspectRatio(contentMode: .fit)
                                .frame(width:200)
                        })
                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                    NavigationLink(
                        destination: Text("CameraView"),
                        label: {
                            Image(systemName: "camera.viewfinder")
                        })
                }
            }
        }
    }
}

暂无
暂无

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

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