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
Encode Struct and Convert to Dictionary [String: Any]
Realitykit - Updating Entity's Translation Returns Unexpected Values
How to Set Local Notifications Between 8Am and 8Pm Every Day
Mapping a Dictionary to an Array of Structs in Swift
Firebase Remove Snapshot Children Swift
Trying to Display Location Data from Firebase to Mapkit
Swift Use 'Is' for Function Type, Compiler Behavior Is Different with Runtime
How to Get Buffer from Avaudioengine's Installtap at High Frequency
Swift: Handling an Unexpected Nil Value, When Variable Is Not Optional
Swift: Method Overriding in Parameterized Class
How to Read a File from The Filesystem in a Swift Command Line App
Creating Semaphore with Initial Value of 0 Make Issues with Execution
Execute Code on Main Thread from Async F#
Macos Swift UI View Where There Is a Search Field in The Title Bar