SwiftUI:在列表滚动中动态移动navigationBar项

时间:2020-05-26 03:02:56

标签: ios swift swiftui swiftui-list

我在我的NavigationBar(SF符号V形)上附加了custom navigationBarItems,当我在ScrollView中滚动浏览我的项目时,NavigationBar将collapse to .inline,并且我的项目保持放置状态。

是否可以将chevron锚定到.navigationBarTitle的右侧,或者在滚动时动态移动项目?

这是我当前的代码:

    NavigationView {
        ScrollView {
            eventView()
        }
        .navigationBarItems(leading:
            Button(action: {
                // action
            }) {
                Image(systemName: "chevron.down.circle")
                    .font(.system(size: 21, weight: .regular))
                    .offset(x: 169, y: 47)
                    .foregroundColor(Color(#colorLiteral(...))
                }
            )
            .navigationBarTitle("Upcoming")
    }

(我是SwiftUI的新手,我知道这还没到头)

非常感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

我不确定您想要什么,但是出现这种情况的原因是您的补偿。

尝试一下:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                ForEach(UIFont.familyNames, id: \.self) { name in
                    Text(name)
                }
                }.navigationBarTitle("Title")
            .navigationBarItems(leading:
                HStack {
                    Button(action: {
                        // action
                    }) {
                        HStack {
                        Text("Upcoming")
                        Image(systemName: "chevron.down.circle")
                             .font(.system(size: 21, weight: .regular))
                        //     .offset(x: 169, y: 47)
                             .foregroundColor(Color.orange)
                        }
                    }

            })
        }
    }
}

enter image description here

enter image description here