首页 > 解决方案 > 视图内容没有出现在 UIScrollView 中

问题描述

我有这个...

func makeUIView(context: Context) -> UIScrollView {
    
    let control = UIScrollView()
 
    control.addSubview(UIHostingController(rootView: preview).view)
 
    return control
}

如果我将preview视图放在自定义滚动视图之外,那么我可以看到它。但是当我将它添加为 a 的子视图时,UIScrollView我什么也看不到。

我是否正确添加了它?

这是我从这里获得的滚动视图的完整代码。

import SwiftUI
import Foundation

struct LegacyScrollView : UIViewRepresentable {
     
    var preview: AnyView
    
    init(preview: AnyView) {
        
        self.preview = preview
    }
    
    func makeCoordinator() -> Coordinator {
        
        Coordinator(self)
    }
 
    func makeUIView(context: Context) -> UIScrollView {
        
        let control = UIScrollView()
 
        control.addSubview(UIHostingController(rootView: preview).view)
 
        return control
    }
 
 
    func updateUIView(_ uiView: UIScrollView, context: Context) {

    }
 
    class Coordinator: NSObject {
        
        var control: LegacyScrollView
 
        init(_ control: LegacyScrollView) {
            
            self.control = control
        }
 
        @objc func handleRefreshControl(sender: UIRefreshControl) {
 
            sender.endRefreshing()
        }
    }
}

标签: swiftswiftui

解决方案


推荐阅读