我正在尝试在 SwiftUI 中创建一个 List
或 Form
来显示一些字段。其中之一是图像。我希望它在单元格中显示没有任何边框,但行的内容似乎在单元格内包含某种我无法摆脱的固有边距。
这是我目前的代码...
List {
Section("Photo") {
Button {
// do a thing
} label: {
Image(uiImage: image)
.resizable()
.aspectRatio(1, contentMode: .fit)
}
}
.listRowBackground(Color.gray)
TextField("Name", text: viewStore.binding(\.$name))
DatePicker("DOB", selection: viewStore.binding(\.$dob), displayedComponents: [.date])
}
这就是它的样子......
这里确实有两个问题。
- 如何消除灰色边框以使图像填充到单元格的范围内?
- 如何使用 UIKit 中的
scaleAspectFill
调整图像大小?它当前正在挤压图像以适应。
最佳答案
好吧...我在输入问题时有了一些想法。
我通过在该部分提供 listRowInsets
解决了这个问题(至少部分解决了)。
Section {
// the content
}
.listRowInsets(.init(top: 0, leading: 0, bottom: 0, trailing: 0))
然后显示为...
现在致力于修复宽高比。 🤣
我使用这个答案修复了宽高比...
关于image - SwiftUI - 在列表/表单中显示图像,单元格周围没有边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69296045/