UIKit
UIKit is general iOS or tvOS UserInterface framework.
Before Swift UI, UIKit construct UserInterface in iOS app.
In this entry, I will explain combination with UIKit and Swift UI
Map
MKMapView is UIKit view.
UIViewRepresentable makeUIView, updateUIView
Actually, what we need to do is wrap UIKit in SwiftUI.
Create struct implement “UIViewRepresentable”
/// Creates a `UIView` instance to be presented. func makeUIView(context: Self.Context) -> Self.UIViewType /// Updates the presented `UIView` (and coordinator) to the latest /// configuration. func updateUIView(_ uiView: Self.UIViewType, context: Self.Context)
Example
This code is from “Creating and Combining Views“
MapView.swift
import SwiftUI import MapKit struct MapView: UIViewRepresentable { func makeUIView(context: Context) -> MKMapView { MKMapView(frame: .zero) } func updateUIView(_ uiView: MKMapView, context: Context) { let coordinate = CLLocationCoordinate2D( latitude: 34.011_286, longitude: -116.166_868) let span = MKCoordinateSpan(latitudeDelta: 2.0, longitudeDelta: 2.0) let region = MKCoordinateRegion(center: coordinate, span: span) uiView.setRegion(region, animated: true) } } struct MapView_Previews: PreviewProvider { static var previews: some View { MapView() } }
コメント