当我添加一个包含 SwiftUI 视图作为 childView 的 UIHostingController,然后将该 childView 放入 UIScrollView 中时,滚动会中断。
在这里我有我的观点
struct TestHeightView: View {
let color: UIColor
var body: some View {
VStack {
Text("THIS IS MY TEST")
.frame(height: 90)
}
.fixedSize(horizontal: false, vertical: true)
.background(Color(color))
.edgesIgnoringSafeArea(.all)
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个 UIViewController 和一个 UIScrollView 作为子视图。UIScrollView 内部有一个 UIStackView,它被正确设置为允许加载 UIView 并在堆栈高度变得足够大时滚动它们。这有效。如果我要加载 40 个 UILabels,它会完美地滚动它们。
当我添加一个普通的旧 UIView,然后在该容器内添加一个 UIHostingController 时,问题就出现了。我这样做是这样的:
let container = UIView()
container.backgroundColor = color.0
stackView.insertArrangedSubview(container, at: 0)
let test = TestHeightView(color: color.1)
let vc = UIHostingController(rootView: test)
vc.view.backgroundColor = .clear
add(child: …Run Code Online (Sandbox Code Playgroud)