SwiftUI:将列表添加到上面有图标的滚动视图中

D J*_*D J 5 swiftui swiftui-list swiftui-view

我正在尝试将项目列表添加到视图中,同时上面有一个图标。目前,当我构建代码时,它将图标和列表分开,这正是我正在寻找的,但它使图标静态且列表可滚动。我希望图标和列表一起移动。

let items = ["Text", "Text", "Text", "Text"]

struct SignInView: View {
    var body: some View {
        NavigationView {
            
            VStack {
                Image(systemName: "car.fill")
                    .font(.system(size: 40))
                    .foregroundColor(.blue)
                    .frame(width: 150, height: 150)
                   
                List {
                    ForEach(items, id: \.self) { item in
                        HStack {
                        Image(systemName: "house.fill")
                                .foregroundColor(.blue)
                        Text(item)
                            Spacer ()
                        Text("1")
                            Image(systemName: "chevron.forward")
                                .foregroundColor(.blue)
                    }
                    }
                    
                }
                
                
                
            }
            
        }.navigationBarTitle("Car", displayMode: .inline)
        
    }
}
Run Code Online (Sandbox Code Playgroud)

ahe*_*eze 4

如果您希望图像随 一起滚动List,则需要将其放入列表中。

struct ContentView15: View {
    let items = ["Text", "Text", "Text", "Text"] /// Note (unrelated to your question): this should be inside ContentView
    
    var body: some View {
        NavigationView {
            List {
                Section { /// separate the image from the rest of the List's contents
                    Image(systemName: "car.fill")
                        .font(.system(size: 40))
                        .foregroundColor(.blue)
                        .frame(width: 150, height: 100)
                        .frame(maxWidth: .infinity) /// make image centered
                        .listRowBackground(Color(.secondarySystemBackground)) /// match the List's background color
                }
                
                ForEach(items, id: \.self) { item in
                    HStack {
                        Image(systemName: "house.fill")
                            .foregroundColor(.blue)
                        Text(item)
                        Spacer ()
                        Text("1")
                        Image(systemName: "chevron.forward")
                            .foregroundColor(.blue)
                    }
                }
            }
            .listStyle(InsetGroupedListStyle()) /// grouped appearance
            .navigationBarTitle("Car", displayMode: .inline)
            /// navigationBarTitle (use navigationTitle for iOS14+) should be *inside* your NavigationView
            /// otherwise, title won't show
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

图像随列表滚动,具有分组样式

  • 感谢您的回答。是否可以将两者分开?这就是我正在寻找的它的样子。(https://i.stack.imgur.com/S4POJ.png) 就像您的示例一样,但列表位于中间,而不是边到边。 (2认同)