So basically I need to grab the exact position of a UIView (CGRect frame) and present a UIHostingController wrapping a SwiftUI View that draws an overlay over said frame (with the help of the .position modifier).
Im currently grabbing the frame of the UIView doing something basic like this:
override func layoutSubviews() {
super.layoutSubviews()
if !isLoading {
let frameToDrawOverlayOn = buttonView.frame
delegate?.presentOverlay(frame: frameToDrawOverlayOn)
}
}
The presentOverlayMethod just presents the overlay:
func presentOverlay(frame: CGRect) {
let vc = UIHostingController(rootView: OverlayView(testFrame: frame))
vc.view.backgroundColor = .clear
vc.modalPresentationStyle = .overCurrentContext
vc.modalTransitionStyle = .crossDissolve
view?.present(vc, animated: false)
}
And the overlay is just:
struct OverlayView: View {
let testFrame: CGRect
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 16)
.stroke(.purple, lineWidth: 4)
.frame(width: testFrame.width, height: testFrame.height)
.position(x: testFrame.midX, y: testFrame.midY)
}
}
}
For whatever reason the positioning of the RoundedRectangle is not correct as it is not above the desired UIView. My guess is its because im grabbing the frame of the UIView which gives me the position of the view but in relation to its parent, how can I make it so I can grab the frame that actually works with the swiftui position modifier so the RoundedRectangle is exactly above the UIView.