iosvisionkitdatascannerviewcontroller

Convert from VisonKit RecognizedItem bounds to view bounds


I'm trying to implement custom highlight views for iOS 16 DataScannerViewController. So far I can add the subviews to the overlayContainerView but I'm struggling with converting from the item bounds to the view bounds.

    func dataScanner(_ dataScanner: DataScannerViewController, didUpdate updatedItems: [RecognizedItem], allItems: [RecognizedItem]) {
        for item in updatedItems {
            if let view = parent.itemHighlightViews[item.id] {
                    
                    view.bounds = item.bounds //Cannot assign value of type 'RecognizedItem.Bounds' to type 'CGRect'
                
            }
        }
    }

How can I position my highlight views correctly?


Solution

  • Ok, in my head I overcomplicated it and the solution is pretty straight forward:

    let rect = CGRect(origin: item.bounds.bottomLeft,
                      size: CGSize(width: item.bounds.topRight.x - item.bounds.topLeft.x,
                                   height: item.bounds.topRight.y - item.bounds.bottomRight.y))
    
    view.frame = rect