将图像与SwiftUI中的navigationBarTitle对齐

时间:2020-06-26 19:32:44

标签: swiftui

我正在尝试在.navigationBarTitle旁边添加一个用户图像(按钮),但是使用下面的代码,该图像出现在标题对齐的顶部。 (附图片)。非常感谢您的帮助!

.navigationBarTitle(Text("Watch"))
                .navigationBarItems(trailing:
                    Image("User")
                        .resizable()
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 36, height: 36)
                        .clipShape(Circle())
                )

Image should be bottom - aligned to the text

2 个答案:

答案 0 :(得分:0)

此代码产生以下视图:

struct ContentView: View {
    
    var body: some View {
        NavigationView {
            List {
                Text("Chocolate")
                Text("Vanilla")
                Text("Strawberry")
            }.navigationBarTitle(Text("Watch"))
            .navigationBarItems(trailing:
                Image(systemName: "person.circle")
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 36, height: 36)
                    .clipShape(Circle())
                    .padding(.top, 90)
            )
        }
    }
}

“项目”有一个默认空间,“文本”有一个默认空间。想象一下,就像一个VStack中的两个HStack。标题在下方的HStack中,项目在上方的HStack中。进入下层没有“真正的”方法。 我建议您创建一个自己的NavigationBar。

enter image description here

答案 1 :(得分:0)

感谢@Simon,我想要的最佳选择是将用户图标添加到标题(而不是NavBar)并应用y的偏移量:-55。向上滚动时,该图标在导航栏下消失。对Apple TV应用程序(移动版)的效果相同。`VStack(对齐方式:.leading){ HStack { 文字(“儿童”) .font(.title) .fontWeight(.bold) .padding(.lead,24) .padding(.top,20)

                        Spacer ()
                        Image("User")
                            .resizable()
                            .aspectRatio(contentMode: .fill)
                            .frame(width: 36, height: 36)
                            .clipShape(Circle())
                            .offset(y: -55)
                            .padding(.trailing, 24)[final result][1]`