SwiftUI ScrollView 没有更新? [英] SwiftUI ScrollView not getting updated?
本文介绍了SwiftUI ScrollView 没有更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目标
获取要显示在 scrollView
预期结果
实际结果
替代方案
使用List
,但是不灵活(不能去掉分隔符,不能有多列)
use List
, but it is not flexible (can't remove separators, can't have multiple columns)
代码
struct Object: Identifiable {
var id: String
}
struct Test: View {
@State var array = [Object]()
var body: some View {
// return VStack { // uncomment this to see that it works perfectly fine
return ScrollView(.vertical) {
ForEach(array) { o in
Text(o.id)
}
}
.onAppear(perform: {
self.array = [Object(id: "1"),Object(id: "2"),Object(id: "3"),Object(id: "4"),Object(id: "5")]
})
}
}
推荐答案
解决这个问题的一个不太老套的方法是将 ScrollView 包含在检查数组是否为空的 IF 语句中
A not so hacky way to get around this problem is to enclose the ScrollView in an IF statement that checks if the array is empty
if !self.array.isEmpty{
ScrollView(.vertical) {
ForEach(array) { o in
Text(o.id)
}
}
}
这篇关于SwiftUI ScrollView 没有更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文