swift - UIScrollView 中的 SwiftUI View 和 UIHostingController 中断滚动
问题描述
当我添加一个包含 SwiftUI 视图的 UIHostingController 作为 childView,然后将该 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)
}
}
然后我有一个 UIViewController 和一个 UIScrollView 作为 subView。在 UIScrollView 内部有一个 UIStackView 正确设置为允许加载 UIViews 并在堆栈高度变得足够大时滚动它们。这行得通。如果我要加载 40 个 UILabel,它会完美地滚动它们。
当我添加一个普通的旧 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: vc, in: container)
func add(child: UIViewController, in container: UIView) {
addChild(child)
container.addSubview(child.view)
child.view.translatesAutoresizingMaskIntoConstraints = false
child.view.topAnchor.constraint(equalTo: container.topAnchor, constant: 0).isActive = true
child.view.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: 0).isActive = true
child.view.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 0).isActive = true
child.view.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: 0).isActive = true
child.didMove(toParent: self)
}
在我的示例中,我添加了 3 个这些 containerViews/UIHostingController,然后添加了一个 UIView(绿色)来演示正在发生的事情。
您可以看到,当我滚动时,所有视图都被暂停,因为形成了一个间隙。正在发生的事情是包含 UIView(浅色)正在扩大其高度。一旦高度达到某个值,滚动将照常继续,直到下一个容器/UIHostingController 到达顶部并再次开始。
我研究过几种不同的解决方案 .edgesIgnoringSafeArea(.all)
做某事。我将它包含在我的示例中,因为没有它,问题完全相同,只是更不和谐,更难用视频解释。基本上发生了同样的事情,但没有任何动画,它只是看起来 UIScrollView 已经停止工作,然后它再次工作
编辑:
我添加了另一个 UIViewController 只是为了确保它通常不是导致问题的孩子。没有。只有 UIHostingControllers 这样做。SwiftUI 中的一些东西
解决方案
令人难以置信的是,这是我能想到的唯一答案:
我在这里的 Twitter 上找到了它https://twitter.com/b3ll/status/1193747288302075906?s=20 by Adam Bell
class EMHostingController<Content> : UIHostingController<Content> where Content : View {
func fixedSafeAreaInsets() {
guard let _class = view?.classForCoder else { return }
let safeAreaInsets: @convention(block) (AnyObject) -> UIEdgeInsets = { (sself : AnyObject!) -> UIEdgeInsets in
return .zero
}
guard let method = class_getInstanceMethod(_class.self, #selector(getter: UIView.safeAreaInsets)) else { return }
class_replaceMethod(_class, #selector(getter: UIView.safeAreaInsets), imp_implementationWithBlock(safeAreaInsets), method_getTypeEncoding(method))
let safeAreaLayoutGuide: @convention(block) (AnyObject) ->UILayoutGuide? = { (sself: AnyObject!) -> UILayoutGuide? in
return nil
}
guard let method2 = class_getInstanceMethod(_class.self, #selector(getter: UIView.safeAreaLayoutGuide)) else { return }
class_replaceMethod(_class, #selector(getter: UIView.safeAreaLayoutGuide), imp_implementationWithBlock(safeAreaLayoutGuide), method_getTypeEncoding(method2))
}
override var prefersStatusBarHidden: Bool {
return true
}
}
推荐阅读
- canvas - 如何在画布上自定义水平条?
- php - 如何将优惠券添加到 WooCommerce 产品页面
- tensorflow - 尝试使用未初始化的值 - 即使我进行了初始化
- cocoapods - SwiftyDropbox 与 Alamofire 问题
- javascript - JavaScript 中的排序功能在某些情况下不能提供正确的输出?
- angular - Angular 6 递归路由定义
- php - PHP使用单值查询
- php - Web 服务器迁移后 LDAP 身份验证不起作用
- javascript - 意外的行为jquery
- javascript - 如何在列表视图 Visual Studio LightSwitch 顶部添加多选复选框