[英]Inserting non-list elements in a SwiftUI view
I am working on a SwiftUI page that consists of a table view with some rows but I would also like to have some non-cell elements in there.我正在处理一个 SwiftUI 页面,该页面由一个带有一些行的表视图组成,但我也想在其中包含一些非单元格元素。 I am currently having some issues with this and I have tried various different avenues.
我目前对此有一些问题,我尝试了各种不同的途径。 I basically just need some elements in there that aren't wrapped inside a cell while still maintaining the tableview's grayish background.
我基本上只需要一些没有包裹在单元格内的元素,同时仍然保持 tableview 的灰色背景。 In my example below, I am trying to get an image right under the table rows.
在下面的示例中,我试图在表格行的正下方获取图像。
Below is my code:下面是我的代码:
import SwiftUI
struct ContentView: View {
private var color = Color(red: 32/255, green: 35/255, blue: 0/255)
var body: some View {
NavigationView {
Form {
Section() {
HStack {
Text("AA")
.foregroundColor(color)
}
HStack {
Text("B")
.foregroundColor(color)
}
HStack {
Text("C")
.foregroundColor(color)
}
HStack {
Text("D")
.foregroundColor(color)
}
HStack {
Text("E")
.foregroundColor(color)
}
HStack {
Text("F")
.foregroundColor(color)
}
HStack {
Text("G")
.foregroundColor(color)
}
}
}.navigationBarTitle(Text("Page"))
HStack {
Image(systemName: "fallLeaves")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Here are all of the scenarios I have tried that have been unsuccessful:以下是我尝试过但未成功的所有方案:
Scenario 1 : Adding a HStack with the image outside of the List.场景 1 :添加带有列表外图像的 HStack。 (What the current code shows) This does not get the image to show as the table view takes the whole view.
(当前代码显示的内容)当表格视图占据整个视图时,这不会显示图像。
Scenario 2 : Adding the HStack with the image within the list block.场景 2 :在列表块中添加带有图像的 HStack。 This wraps the image around the cell like so
像这样将图像包裹在单元格周围
The ideal solution would look like this but I'm not sure what to try as I can't get it to look like this where it's all one continuous view and the image isn't in a cell理想的解决方案看起来像这样,但我不确定要尝试什么,因为我无法让它看起来像这样,它是一个连续视图并且图像不在单元格中
i'll let you work out the padding and the rounding.我会让你算出填充和舍入。
struct ContentView: View {
private var color = Color(red: 32/255, green: 35/255, blue: 0/255)
var body: some View {
NavigationView {
Form {
Section(content: {
HStack {
Text("AA")
.foregroundColor(color)
}
HStack {
Text("B")
.foregroundColor(color)
}
}, footer: {
Image("fallLeaves")
})
}.navigationBarTitle(Text("Page"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You can also use place your component (event a page view) inside the cell.您还可以将您的组件(事件页面视图)放在单元格中。
What can be done;可以做什么;
.listRowBackground(Color.clear)
to the new view;.listRowBackground(Color.clear)
到新视图; to see the background transparently..listRowSeparator(.hidden)
to the new view;.listRowSeparator(.hidden)
添加到新视图; to remove the cell line below. After these, you'll have a placed view with additional padding.在这些之后,您将拥有一个带有额外填充的放置视图。 If this is important, you can play with padding to catch 0 padding distance.
如果这很重要,您可以使用填充来捕捉 0 填充距离。 Also, by changing the list style (for this second group) with these kinds of styles,
.listStyle(.grouped)
, you can get catch zero padding/spacing.此外,通过使用 styles,
.listStyle(.grouped)
更改列表样式(对于第二组),您可以获得零填充/间距。
The footer solution can work for images, but when you need some texts, you'll see that it'll scale down the sizes.页脚解决方案适用于图像,但当您需要一些文本时,您会发现它会按比例缩小尺寸。 So instead of footer, my solution is to use a cell view and show it as a non-cell item;
因此,我的解决方案不是使用页脚,而是使用单元格视图并将其显示为非单元格项;
Here is my change, which works (at least I think)这是我的零钱,有效(至少我认为)
import SwiftUI
struct ContentView: View {
private var color = Color(red: 32/255, green: 35/255, blue: 255/255)
// Just changed color to test my changes better.
// private var color = Color(red: 32/255, green: 35/255, blue: 0/255)
var body: some View {
NavigationView {
Form {
Section() {
Text("AA")
Text("B")
Text("C")
Text("D")
Text("E")
Text("F")
Text("G")
}
//foreground color won't colorize the cell's bottom lines
.foregroundColor(color)
Section{
HStack{
// Use Spacer or another horizontal alignment
// I just tried with another image, you can place yours with full width and remove the Spacer() 's
Spacer()
Image(systemName: "checkmark.seal.fill")
.resizable()
.scaledToFit()
.foregroundColor(color)
.frame(width: 100)
Spacer()
}
.listRowBackground(Color.clear)
// Not required for single items; but would be usefull, if you'll add more views or sections
.listRowSeparator(.hidden)
}
}.navigationBarTitle(Text("Page"))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
And here is why your methods won't work;这就是为什么您的方法不起作用的原因;
Scenario 1: Using VStack and HStack will split the screen into different scrollable.场景 1:使用 VStack 和 HStack 会将屏幕拆分为不同的滚动条。 This is by design, In SwiftUI
List
element overrides Stacks and ScrollableView
这是设计使然,在 SwiftUI 中,
List
元素会覆盖 Stacks 和ScrollableView
Scenario 2: Same as Scenario 1场景二:同场景一
Scenario 3: Actually, this is the same as Scenario 1场景三:其实这和场景一是一样的
You might try 3 things;你可以尝试三件事;
ZStack
and develop a custom button object which looks like a cell view (cons: non-native way and long time consuming work around)ZStack
中确定背景颜色并开发一个看起来像单元格视图的自定义按钮 object(缺点:非本地方式和耗时的解决方法)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.