https://kotlinlang.org logo
l

Lukas Anda

09/12/2023, 9:40 PM
Has anyone encountered this issue when using custom swiftui view wrapped in UIKit wrapped in compose?
[SwiftUI] Accessing Environment<CGFloat>'s value outside of being installed on a View. This will always read the default value and will not update.
This is my implementation of the view:
Copy code
struct TestView: View {
    var body: some View {
        VStack(alignment: .leading) {
            
            LaTeX("The quadratic formula is $$x=\\frac{-b\\pm\\sqrt{b^2-4ac}}{2a}$$ and it has zeros at the roots of $f(x)=ax^2+bx+c$.")
              .blockMode(.alwaysInline) // This method call actually causes the error because it modifies the environment variable of LaTeX
              .font(.system(size: 16))
              .frame(maxWidth:.infinity, alignment: .leading)
              .background(.green)
        }
        .frame(alignment: .leading)
        .frame(maxWidth: .infinity)
        .background(.red)
    }
}

class TestViewInterfaceImpl: TestViewInterface {
    private var textView: UITextView?
    func getView() -> UIView {
        let controller = UIHostingController(rootView: TestView())
        let targetSize = controller.sizeThatFits(in: CGSize(width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height))
        controller.view.frame = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height:targetSize.height))
        
        let view = UIView(frame: CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width, height:targetSize.height)))
        view.addSubview(controller.view)
        return view
    }
    
    func showText(text: String) {
        
    }
    
}
i

Ivan Matkov

09/13/2023, 6:57 AM
Well, it's clearly just SwiftUI/Compose-unrelated problem. I guess the error message gives good direction what to find here - check using floats outside a View
l

Lukas Anda

09/13/2023, 6:58 AM
Well, it is not using any haha. But will try to ask on swiftui forums
👍 1