View SwiftUI:如何按给定宽度获取HostingController视图/内容最大高度?

View SwiftUI:如何按给定宽度获取HostingController视图/内容最大高度?,view,swiftui,pdf-generation,viewrendering,uihostingcontroller,View,Swiftui,Pdf Generation,Viewrendering,Uihostingcontroller,我试图获取UIHostingController视图/内容的最大高度,并且只知道宽度。 我想基于我的视图创建一个PDF文档。一切正常,但我不知道如何获取视图/内容的最大高度,因此PDF知道我的视图/内容有多少页“长” func exportToPDF() { let pdfName = "document" let documentDirectory = FileManager.default.urls(for: .docum

我试图获取UIHostingController视图/内容的最大高度,并且只知道宽度。 我想基于我的视图创建一个PDF文档。一切正常,但我不知道如何获取视图/内容的最大高度,因此PDF知道我的视图/内容有多少页“长”

func exportToPDF() {

        let pdfName = "document"
       
        let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let outputFileURL = documentDirectory.appendingPathComponent("\(pdfName).pdf")
        
        let printView = ProjectDetailView(viewModel: viewModel)
        let pdfVC = UIHostingController(rootView: printView)
        
//Render the view behind all other views
        let rootVC = UIApplication.shared.windows.first?.rootViewController
        rootVC?.addChild(pdfVC)
        rootVC?.view.insertSubview(pdfVC.view, at: 0)
        
        let height: CGFloat = ???? <- **Needed Height by given width of 8.5 * 72.0** 
        let width: CGFloat = 8.5 * 72.0
        
        pdfVC.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
        

        let numberOfPagesThatFitVertically = Int(ceil(height / 842))
        
        //Render the PDF
        let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: 8.5 * 72.0, height: height))
        
        do {
            try pdfRenderer.writePDF(to: outputFileURL, withActions: { (context) in
                
                for indexVertical in 0 ..< numberOfPagesThatFitVertically
                {
                    
                    let offsetVerticalFloat = CGFloat(indexVertical) * 842
                    let offsetVertical = CGRect(x: 0, y: -offsetVerticalFloat, width: 8.5 * 72.0, height: 842)
                    
                    
                    context.beginPage(withBounds: offsetVertical, pageInfo: ["Page" : indexVertical])
                    
                    pdfVC.view.layer.render(in: context.cgContext)
                }
                
            })
            
            self.fileURL = outputFileURL
            
            
        }catch {
            //        self.showError = true
            print("Could not create PDF file: \(error)")
        }
        
        pdfVC.removeFromParent()
        pdfVC.view.removeFromSuperview()
        
  
    }

以下是可能的方法:

let width: CGFloat = 8.5 * 72.0
let height: CGFloat = pdfVC.view.sizeThatFits(
     CGSize(width: width, height: .greatestFiniteMagnitude)).height

以下是可能的方法:

let width: CGFloat = 8.5 * 72.0
let height: CGFloat = pdfVC.view.sizeThatFits(
     CGSize(width: width, height: .greatestFiniteMagnitude)).height

谢谢你的回答。你的方法给出的高度是86.6005859375,但应该是1200左右。。。我不知道86号在哪里。。。是从哪里来的,但和实际尺寸相差甚远。有什么想法吗?这取决于视图内容。如果
ProjectDetailView
不是静态的,而是异步填充(构建)的,那么您需要在计算大小之前以某种方式对其进行准备,以便准备就绪。ProjectDetailView是一个“正常”的SwiftUI视图。我在问题中加入了这个观点。我可以使SwiftUI视图保持静态吗?或者如何在SwiftUI中准备视图?好的,我发现了问题。打印视图中包含一个geometryReader,这是什么导致的。最大有限大小读取的高度错误。不知道这是一个bug还是一个特性。再次感谢!谢谢你的回答。你的方法给出的高度是86.6005859375,但应该是1200左右。。。我不知道86号在哪里。。。是从哪里来的,但和实际尺寸相差甚远。有什么想法吗?这取决于视图内容。如果
ProjectDetailView
不是静态的,而是异步填充(构建)的,那么您需要在计算大小之前以某种方式对其进行准备,以便准备就绪。ProjectDetailView是一个“正常”的SwiftUI视图。我在问题中加入了这个观点。我可以使SwiftUI视图保持静态吗?或者如何在SwiftUI中准备视图?好的,我发现了问题。打印视图中包含一个geometryReader,这是什么导致的。最大有限大小读取的高度错误。不知道这是一个bug还是一个特性。再次感谢!