Ios 添加导航按钮Bartitle Swift ui

Ios 添加导航按钮Bartitle Swift ui,ios,swift,swiftui,Ios,Swift,Swiftui,我在swift ui中工作。我想在导航栏标题的旁边放一个按钮 我希望能够单击用户图像并导航到另一个视图 可以这样做吗?试试这个 NavigationView{ .navigationBarTitle("Browse") .navigationBarItems(trailing: Button(action: { // Move to next View }){ Image() } ) } 使用.navigatio

我在swift ui中工作。我想在导航栏标题的旁边放一个按钮

我希望能够单击用户图像并导航到另一个视图

可以这样做吗?

试试这个

NavigationView{
    .navigationBarTitle("Browse")
    .navigationBarItems(trailing:
        Button(action: { // Move to next View }){
            Image()
        }
    )
}

使用
.navigationBarItems()
将按钮放置在导航栏中。任何视图都可以在
按钮
内使用,因此与图像中的按钮相似的按钮可以声明为:

var body: some View {
    NavigationView {

        // the rest of your UI components

        .navigationBarTitle("Browse")
        .navigationBarItems(trailing: Button(action: {}) {
                                VStack {
                                    Spacer()
                                    Image("name")
                                        .resizable()
                                        .frame(width: 45, height: 45)
                                        .clipShape(Circle())
                                }
                            })
    }
}

请注意,它的对齐方式稍有不同(将绘制得比您的示例高一点)。

这应该可以解决偏移的问题,但它非常粗糙。也许对你的问题有比这更好的答案。但如果你同意这个答案,请给卢卢加加一张赞成票,因为我从他那里抄了很多。我自己也没有想到这个答案,但我记不起在哪里找到了最初的答案

NavigationView {

    // the rest of your UI components

    .navigationBarTitle("") // To hide the real navigationBarTitle
    .navigationBarItems(leading:
        Text("Browse").font(.largeTitle).bold().padding(.top, 10), // To add a fake navigationBarTitle
        trailing: Button(action: {}) {
            VStack {
                Spacer()
                Image("swiftui")
                    .resizable()
                    .frame(width: 45, height: 45)
                    .clipShape(Circle())
            }
        } .buttonStyle(PlainButtonStyle()) // You should also add that to your code otherwise the picture will turn blue
    )
}

是的,这是可以做到的。这里的例子是:OP正在询问SwiftUI的解决方案。我注意到对齐有点偏离了。不幸的是,你对此无能为力。导航栏中的按钮与顶部对齐。垫片()将其尽可能向下推。欢迎使用堆栈溢出。如果你能解释一下为什么你的代码能解决这个问题,那会很有帮助。请尝试解释您的步骤,或包含参考原始文档的部分文档。
var body: some View {
NavigationView {
    Text("SwiftUI")
        .navigationBarTitle("Welcome")
        .navigationBarItems(trailing:
            Button("Bar button") {
                print("Bar button!")
            }
        )
}