Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取SwiftUI视图大小并将其写入模型的正确方法_Swift_Macos_Swiftui - Fatal编程技术网

获取SwiftUI视图大小并将其写入模型的正确方法

获取SwiftUI视图大小并将其写入模型的正确方法,swift,macos,swiftui,Swift,Macos,Swiftui,我有一些模型: public struct someModel{ var someContent: CGSize } 我有一些看法: struct StatusEntryView: View { @ObservedObject var model : someModel var body: some View { ScrollView([Axis.Set.horizontal, Axis.Set.vertical]) { Ge

我有一些模型:

public struct someModel{
    var someContent: CGSize
}
我有一些看法:


struct StatusEntryView: View {
    @ObservedObject var model : someModel

    var body: some View {
        ScrollView([Axis.Set.horizontal, Axis.Set.vertical]) {
            GeometryReader { gr in
                VStack{
                    ForEach(self.model.hunks){ hunk in
                        HunkView(hunk: hunk)
                    }
                }

            self.model.contentSize = gr.size
            }
        }
    }
}

获取ScrollView的实际内容大小并将其写入模型的正确方法是什么?

可以使用
GeometryReader
完成,但不在该位置,在这里

ScrollView([Axis.Set.horizontal,Axis.Set.vertical]){
VStack{
ForEach(self.model.hunks){hunkin
HunkView(帅哥:帅哥)
}
}.background(GeometryReader{gr in
self.model.contentSize=gr.size//可能需要异步包装
返回矩形().fill(Color.clear)
})
}

注意:请注意代码中的注释,
因为现在还不清楚如何使用该大小,但是如果您要立即更改视图的布局,则需要将其包装在
DispatchQueue.main.async{}`为避免视图更新循环。

稍后将进行检查,并在检查后将您的答案标记为正确。非常感谢,阿斯佩里