SwiftUI-细胞分解项目

您可以将其完全复制到操场上进行复制。当您启动它时,预览看起来不错,但是当您开始上下滚动时,项目(HStack)消失了。我不知道为什么...这是苹果的错误还是我的误解?

  //: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport
import SwiftUI

struct Cell : View {

    var country: String

    var body: some View {
        VStack() {
            Text("OMG").onAppear() { print ("OMG") }
            ScrollView(.horizontal) {
                HStack() {
                    Text(country)

                }.background(Rectangle().fill(Color.blue))//.frame(height: 195)
            }.background(Rectangle().fill(Color.orange))//.frame(height: 205)

        }
    }
}

struct ContentView: View {

    var countries = ["a","b","c","d","e","f","g","h"]

    init() {
        UITableView.appearance().showsVerticalScrollIndicator = false
        UITableView.appearance().showsHorizontalScrollIndicator = false
    }

    var body: some View {

        GeometryReader() { geometry in
            VStack() {
                Text("Title")
                List(self.countries,id: \.self) { country in

                    Cell(country: country)

                }.environment(\.defaultMinListRowHeight,340)
            }
        }
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())
zdp888 回答:SwiftUI-细胞分解项目

根据我的调查,问题是在List内使用ScrollView(也许是Apple的问题)。作为解决方法(或替代方法),您可以考虑使用ScrollView替换List,如下所示(ContentView的主体,您可以在想要具有类似外观的位置手动添加Divider()):

var body: some View {

    GeometryReader() { geometry in
        VStack() {
            Text("Title")
            ScrollView(.vertical) {
                ForEach(self.countries,id: \.self) { country in
                    Cell(country: country)
                        .frame(height: 340)
                }
            }
        }
    }
}
本文链接:https://www.f2er.com/3160741.html

大家都在问