简体   繁体   English

在 SwiftUI 视图中设置高度的问题

[英]Issues setting height in SwiftUI View

I've implemented the design below, but I've got a bug that seems trivial to figure out the solution.我已经实现了下面的设计,但是我有一个似乎微不足道的错误来找出解决方案。 The code below can draw the view and be able to display the data correctly, only when I get the data locally, but not when I get the data over the network.下面的代码可以绘制视图,并且能够正确显示数据,只有当我在本地获取数据时,而不是当我通过网络获取数据时。 The view doesn't draw the card I have to click on another tab and come back to the view with the card.视图没有绘制卡片我必须单击另一个选项卡并返回带有卡片的视图。 I tried to figure out what the issue might be, and I was able to find out this code to be the source .frame(minHeight: 0, maxHeight: .infinity) , it sets the height.我试图弄清楚问题可能是什么,并且我能够找出这个代码是源代码.frame(minHeight: 0, maxHeight: .infinity) ,它设置了高度。 If I remove this line I am able to see the card, but with a shanked height.如果我删除这条线,我可以看到卡片,但有一个小腿高度。 I tried to change the height to the static value, but it didn't work.我尝试将高度更改为 static 值,但没有成功。 I'll appreciate your help.我会感谢你的帮助。

View One查看一

ScrollView(.horizontal, showsIndicators: false){
    HStack(spacing: 20){
        ForEach(someIterator) { someViewModel in
            CardView(myViewModel: someViewModel)
        }
    }.padding([.leading,.bottom,.trailing])
}
.frame(height: 420)

View Two查看二

struct CardView: View {
    @ObservedObject var myViewModel: MyViewModel
    var body: some View {
        VStack(){
            HStack(){
                Text(myViewModel.mymodel.value)
                Text("Some text")
            }
            Text(myViewModel.mymodel.description)

        }
        .frame(minWidth: 0 , maxWidth: 327, maxHeight: 380)
        .frame(minHeight: 0 , maxHeight: .infinity) // The issue
        .background(Color("Blue"))
        .cornerRadius(10)
    }
}

数据

ScrollView needs to know content size in-advance, that's why you observe issue with getting data from network, because in such case there is no initial content in scroll view and it is squeezed ScrollView需要提前知道内容大小,这就是为什么您观察到从网络获取数据的问题,因为在这种情况下,滚动视图中没有初始内容并且它被挤压

Here is a solution, tested with replicated code on Xcode 11.4 / iOS 13.4这是一个解决方案,在 Xcode 11.4 / iOS 13.4 上使用复制代码进行了测试

ScrollView(.horizontal, showsIndicators: false){
    HStack(spacing: 20){
        ForEach(someIterator) { someViewModel in
            CardView(myViewModel: someViewModel)
        }
    }.padding([.leading,.bottom,.trailing])
    .frame(height: 420)                      // << move inside !!
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM