SwiftUI: 如何创建一个忽略HStack最大宽度的列表

huangapple go评论58阅读模式
英文:

SwiftUI: How to make a list that ignores the max of HStacks

问题

以下是您要翻译的内容:

"I would like to create a list like this
But as soon as I add more than 10 elements I get the error
"Extra argument in call"
I know this error happens because I have too many Stacks in my View

SwiftUI: 如何创建一个忽略HStack最大宽度的列表

What would be the best way to build a list like this that is scrollable and I can add Views with different values that are stylized?

My code so far

List{
    CardView()
    CardView()
    CardView()
    CardView()
    CardView()
}

And the CardView is

struct CardView: View {
    var body: some View {
        HStack{
            Image(systemName: "circle.hexagongrid")
            Divider()
            Text("Text")
                .frame(minWidth: 100)
            Divider()
            Spacer()
            Text("1111")
        }
    }
}

Is there a better option in SwiftUI to display lists like this?
I would also like to have a bit of space between the CardViews, but I can figure that out later when I know how to build my list."

英文:

I would like to create a list like this
But as soon as I add more then 10 elements I get the error
"Extra argument in call"
I know this error happens because I have to many Stacks in my View

SwiftUI: 如何创建一个忽略HStack最大宽度的列表

What would the best way be to build a list like this that is scrollable and I can add Views With different values that are stylized?

my code so far

List{
    CardView()
    CardView()
    CardView()
    CardView()
    CardView()
}

and the CardView is

struct CardView: View {
var body: some View {
    HStack{
        Image(systemName: "circle.hexagongrid")
        Divider()
        Text("Text")
            .frame(minWidth: 100)
        Divider()
        Spacer()
        Text("1111")
    }
}

}

is there a better option in SwiftUI to display lists like this?
I would also like to have a bit of space between the CardView's but I can figure that out later when I know how to build my list.

答案1

得分: 1

你可以通过使用 ForEach 来实现这个功能。

struct ContentView: View {
    
    var body: some View {
        List {
            ForEach(0..<30) { item in
                CardView()
            }
        }
    }
}

30 是将要创建的视图的数量。

希望这有所帮助 SwiftUI: 如何创建一个忽略HStack最大宽度的列表

英文:

You can achieve this by using ForEach

struct ContentView: View {
    
    var body: some View {
        List {
            ForEach(0..&lt;30) { item in
                CardView()
            }
        }
    }
}

30 is the number of views that will get created

I hope this helps SwiftUI: 如何创建一个忽略HStack最大宽度的列表

huangapple
  • 本文由 发表于 2023年3月7日 04:52:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655732.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定