SwiftUI 2,s Mapkit MKPointOfInterestFilter - swiftui

Is there any way to filter the point of interest using SwiftUI 2 Mapkit View
https://developer.apple.com/documentation/mapkit/map
https://developer.apple.com/documentation/mapkit/mkmapview/3143417-pointofinterestfilter
I want to do something like this
let filter = MKPointOfInterestFilter(including: [.cafe])
mapView.pointOfInterestFilter = filter
But using the new
Map(coordinateRegion: <#T##Binding<MKCoordinateRegion>#>,
interactionModes: <#T##MapInteractionModes#>,
showsUserLocation: <#T##Bool#>,
userTrackingMode: <#T##Binding<MapUserTrackingMode>?#>,
annotationItems: <#T##RandomAccessCollection#>,
annotationContent: <#T##(Identifiable) -> MapAnnotationProtocol#>)

Related

Navigation bar title appears with delay in mixed UIKit and SwiftUI project

I'm presenting a SwiftUI view from UIViewController.
let vc = UIHostingController(rootView: SwiftUIView())
navigationController?.pushViewController(vc, animated: true)
struct SwiftUIView: View {
var body: some View {
VStack {
}
.navigationBarTitle("Title")
}
}
The title appears only after the view has appeared. How to fix?
If you use UINavigationController then you need to set the title on the UIHostingController for it to appear instantly. Otherwise the navigation title will only appear after the SwiftUI view is rendered.
You can set the navigation title of any UIViewController using title
let vc = UIHostingController(rootView: SwiftUIView())
vc.title = "Title"
navigationController?.pushViewController(vc, animated: true)

SwiftUI MapKit [Memory] Resetting zone allocator with 24 allocations still alive

I'm developing an Ipad app that contains a MapKit Map using SwiftUI and I get the following error message in the XCode console everytime when the view with the map disappears:
[Memory] Resetting zone allocator with 24 allocations still alive.
Thankful for any help on how to solve it. :)
Related threads
First, before posting some code, let me say that there are a few threads related to this error. I've looked at this SO thread, Resetting zone allocator with allocations still alive, but it relates to UIKit and not to SwiftUI and either way I get the error message without adding annotations or zooming.
Another related thread here on SO is SwiftUI - EnvironmentObject - Strange Memory Usage but there the OP uses MKMapView instead of SwifUI's Map. Preferably, I'd like to use Map, but if nothing else MKMapView might be a way forward.
There is also some threads on the Apple developer forum, such as this one, but I've found no complete matches (in the linked thread, the OP only gets the error once, while it reoccurs for me every time the view with the map disappears).
Minimal reproducible example
The error occurs each time the view button that removes the Map is pressed.
View creating the error
import MapKit
import SwiftUI
struct ContentView: View {
#State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))
#State private var showMap = true
var body: some View {
if showMap {
VStack {
Map(coordinateRegion: $region)
Button("Hide map") {
showMap = false
}
.font(.title)
}
} else {
Button("Show map") {
showMap = true
}
.font(.title)
}
}
}
Set-up view
import SwiftUI
#main
struct SwiftUIByExampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

What is the alternative of applicationDidReceiveMemoryWarning in UIKit for SwiftUI?

I want to control low memory state in my SwiftUI app.
What is the alternative of applicationDidReceiveMemoryWarning in UIKit for SwiftUI when you choose SwiftUI lifecycle?
I found didReceiveMemoryWarningNotification for Notification Center, but it is also implemented on UIKit, not on Foundation.
Or if there is no alternative, should I use UIKit lifecycle?
Thanks
You can use a Combine publisher which detects UIApplication.didReceiveMemoryWarningNotification notifications.
Example:
struct ContentView: View {
private let memoryWarningPublisher = NotificationCenter.default.publisher(for: UIApplication.didReceiveMemoryWarningNotification)
var body: some View {
Text("Hello world!")
.onReceive(memoryWarningPublisher) { _ in
print("memory warning")
}
}
}
Result:

iOS 15 Status Bar Background Color in SwiftUI/UIKit Nav mix

We have an app which uses a mix of UIKit and SwiftUI. The nav hierarchy is:
UIKit Tab Bar > UIKit Table > SwiftUI View > UIKit View
as of iOS 15 the status bar is not taking on the color of the nav bar:
I'm trying to figure out if this is something I can fix.
Looking in Reveal it looks as tho the UIHostingView has a white bg:
Is it possible write code to change the bg color of the status bar in this setup?
This doesn't seem possible in a UIViewControllerRepresentable? And the hosted view is not taking up the whole screen, so changing that has no impact(?)
import SwiftUI
import PaddleCloud
struct PaddleMapView: UIViewControllerRepresentable {
typealias UIViewControllerType = MapViewController
var trip: Trip
var tripSpeeds: TripSpeeds
func makeUIViewController(context: Context) -> MapViewController {
let sb = UIStoryboard(name: "Map", bundle: nil)
let mapVC = sb.instantiateInitialViewController() as! MapViewController
mapVC.trip = trip
mapVC.tripSpeeds = tripSpeeds
return mapVC
}
}
NB: Changing the bg color in reveal does not change anything in the app.

How can call UIViewController to SwiftUI View

I am try to learn SwiftUI . Try to implement SwiftUI code . I am not clear . please anyone help me.
1) Any view controller to other view Controller . it is no problem .
let vc = viewController.init(nibName: "VCNibName", bundle: nil)
self.navigationController?.pushViewController(vc, animated: true)
or
let newViewController = storyBoard.instantiateViewController(withIdentifier: "VCIdentifier")
self.navigationController?.pushViewController(newViewController, animated: true)
2) Any view controller to SwiftUI view .
how can push SwiftUIView to navigationController?
Use UIHostingController to wrap your View
let hostingController = UIHostingController(rootView: MySwiftUIView())
self.navigationController?.pushViewController(hostingController, animated: true)