Understanding UIviewrepresentable

Understanding UIViewRepresentable

Just make your Coordinator is a NSObject, it usually plays bridge/controller/delegate/actor role, but not presentation, so should not be is-a-UIView

final class Coordinator: NSObject {
private let view: UIView

init(_ view: UIView) {
self.view = view
}

and one more...

func makeUIView(context: Context) -> UIView {
// make target a coordinator, which is already present in context !!
let tapGesture = UITapGestureRecognizer(target: context.coordinator,
action: #selector(Coordinator.handleTap(sender:)))
v.addGestureRecognizer(tapGesture)
return v
}

Do something as soon as UIViewRepresentable is instantiated

It is possible but would be not correct (so I will not tell even how), instead use it

struct OverlayMapView: UIViewRepresentable {
@StateObject private var viewModel = MapViewModel()

func makeUIView(context: Context) -> MKMapView {
viewModel.safeCreateLocationManager() // << here !!

// view creation is next ...

*the makeUIView is called after init and before onAppear

Chain methods with UIViewRepresentable subclass

These "chained methods" are not supposed to be mutating. If you look at the methods that you can chain in the SwiftUI framework, none of them are marked mutating. Quick example.

These methods are supposed to return a new instance of the struct, with some property changed.

i.e. like this:

func editable(_ editable: Bool) -> TextView {
// This is a new instance of the struct, but _editable is changed to the new value
TextView(text: $text, _editable: editable)
}

UIViewRepresentable hidden behind SwiftUI View

The problem seemed to be with intrinsicContentSize. I changed in in version 2.3.3 of the control, and now fixedSize() works fine.

Connect UIViewRepresentable to SwiftUI

So to get around this first make your app has permission to access the users camera(go to Info.plist or info tab beside the build settings at the top and add Privacy camera usage and add "We need your camera to perform this action")

After that a simple call in the sheet's modifier should do the trick

struct ContentView: View {
//@State private var image: Image?
@State private var showingCameraSession = false
//@Binding var isShown: Bool
var body: some View {
VStack{

// ControlButton(systemIconName: "slider.horizontal.3"){
Button("Seelect Image") {
showingCameraSession = true
} .sheet(isPresented: $showingCameraSession){
//What to place here?

CameraSession()


}

}
}
}


Related Topics



Leave a reply



Submit