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)
如果您希望图像随 一起滚动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)
结果:
| 归档时间: |
|
| 查看次数: |
2833 次 |
| 最近记录: |