Swift MapKit MKAnnotationView button not working - swift

I have a custom MKAnnotationView that contains a button outlet which displays on the map when a user taps a pin. I'm struggling to make the button tap event work as it never gets called and instead the MKAnnotationView disappears. How can I make the button work?
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if view.annotation is MapMarker {
return
}
if let waypointAnnotation = view.annotation as? WaypointAnnotation {
let calloutView = self.createCalloutView(annotation: waypointAnnotation)
calloutView.removeOutlet.addTarget(self, action: #selector(removeWaypoint(id:)), for: .touchUpInside)
waypointAnnotation.calloutView = calloutView
calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height*0.52)
view.addSubview(calloutView)
}
}
Create calloutView function:
private func createCalloutView(annotation: WaypointAnnotation) -> WaypointCalloutView {
let views = Bundle.main.loadNibNamed("WaypointCalloutView", owner: nil, options: nil)
let calloutView = views?[0] as! WaypointCalloutView
calloutView.id = annotation.id
calloutView.delegate = self
return calloutView
}
Thanks.

Related

Custom MKAnnotationView want to be tapped again

I'm having problems with my custom MKAnnotationView.
I download the coordinate where to place it from my REST api and I place the pin on map with this code:
private func refreshMapView(andCenterMap: Bool){
DispatchQueue.main.async { [weak self] in
guard let self = self else {
return
}
self.mapView.addAnnotations(self.spotsArray)
if andCenterMap {
self.centerMap(at: self.mapView.userLocation.coordinate)
}
}
}
Ones the pin is placed I zoom automatically the map.
Here the code for the custom annotation creation:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// Do not touch the annotation of the user location
if annotation.isKind(of: MKUserLocation.self){
return nil
}
let annoIdentifier = "SPOT"
var annotationView: MKAnnotationView?
if let dequeued = mapView.dequeueReusableAnnotationView(withIdentifier: annoIdentifier) {
annotationView = dequeued
}else{
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annoIdentifier)
annotationView = av
}
// Changing the image of the pin
annotationView!.annotation = annotation
if let image = UIImage(named: "map_pin") {
annotationView!.image = image
let deltaY = image.size.height/2
annotationView!.centerOffset = CGPoint(x: 0.0, y: -deltaY)
}else{
annotationView!.image = nil
}
return annotationView
}
As you can notice, my custom pin is using this image (#1x, #2x, #3x)
Ones tapped I want to show the "detail view" and I use the annotation as a sender in order to have all the information I need on the next view.
Here the code:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
guard let ann = view.annotation else {
return
}
if ann.isKind(of: MKUserLocation.self){
return
}
self.performSegue(withIdentifier: "showDetailSegue", sender: ann)
}
So I present all the data I need to and I can go back to the "map view".
The problem is:
In the map view, if I want to select the same annotation again, this is not tappable anymore.
I can see it (in the right place) but I can select it again only if I zoom a little bit and I tap "around" the annotation.
Any suggestione how to solve this issue?
Thanks in advance!
Add this end of didSelect
mapView.deselectAnnotation(ann,animated:false)

Swift: Button Action on Custom CalloutView

I am implementing a custom calloutView on my mapView and I am attempting to implement action buttons from the custom calloutView. I am having trouble finding ways to detect the buttons tapped with respect to the calloutViews.
Below are my implementation:
//At MapViewController
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !annotation.isKind(of: Post.self) {
return nil
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView?.canShowCallout = true
annotationView?.animatesDrop = true
} else {
annotationView?.annotation = annotation
}
let postAnnotation = annotation as! Post
let calloutView = DetailCalloutView()
calloutView.titleLabel.text = ...
calloutView.timestampLabel.text = ...
calloutView.postContentTextView.text = ...
calloutView.profileImageView.image = ...
calloutView.deleteButton.addTarget(self, action: #selector(deleteButtonTap), for: .touchUpInside)
calloutView.chatButton.addTarget(self, action: #selector(chatButtonTap), for: .touchUpInside)
annotationView?.detailCalloutAccessoryView = calloutView
return annotationView
}
#objc func deleteButtonTap(_ sender: MKAnnotationView) {
let post = sender.annotation as? Post
let index = postArray.index(of: post) //Is there a way to implement something like this here??
...
}
#objc func chatButtonTap() {
print("123")
}
//At Post
class Post: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var post: String?
//All other properties
init(coordinate: CLLocationCoordinate2D, post: String, ...) {
self.coordinate = coordinate
self.post = post
...
}
}
//At DetailCallOutView
class DetailCalloutView: UIView {
let containerView: UIView = {
...
return v
}()
//All other UI elements
let chatButton: UIButton = {
let b = UIButton()
...
return b
}()
let deleteButton: UIButton = {
let b = UIButton()
...
return b
}()
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
func setupViews() {
addSubview(containerView)
//adding subviews for all UI elements
containerView.addSubview(cButton)
containerView.addSubview(dButton)
//setting up constraints for all UI elements
}
}
I am able to detect the chat and delete buttons, but am unable to obtain the post with respect to the postArray or annotation. Is there a way to do so?
First action is of type that target added to , which is UIButton here
calloutView.deleteButton.tag = <#setToIndexOfArr#>
calloutView.deleteButton.addTarget(self, action: #selector(deleteButtonTap), for: .touchUpInside)
//
#objc func deleteButtonTap(_ sender: UIButton) {
let item = modelArr[sender.tag]
}

Swift Parse MapView Not Displaying CalloutAccessory and Segueing

I currently have a UIMapView with different annotations pinned for the different locations of users on the map. I'm trying to segue to the respective users profile when the annotation is clicked. Here's my code to set up the annotations:
let query = PFQuery(className: "location")
query.findObjectsInBackground { (objects, error) in
if let brandsLocation = objects {
for object in brandsLocation {
if let brandLocation = object as? PFObject {
let annotation = MKPointAnnotation()
let annotationPoint = object["geoPoint"] as! PFGeoPoint
annotation.coordinate = CLLocationCoordinate2DMake(annotationPoint.latitude, annotationPoint.longitude)
annotation.title = brandLocation["annotationTitle"] as? String
annotation.subtitle = brandLocation["annotationSubtitle"] as? String
self.map.addAnnotation(annotation)
}
}
}
}
This is where I believe the segue is supposed to happen, however it isn't doing anything for some reason.
// When user taps on the disclosure button you can perform a segue to navigate to another view controller
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView{
performSegue(withIdentifier: "goToUserProfile", sender: view)
print(view.annotation?.title) // annotation's title
print(view.annotation?.subtitle) // annotation's subttitle
//Perform a segue here to navigate to another viewcontroller
// On tapping the disclosure button you will get here
}
}
// Here we add disclosure button inside annotation window
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
print("viewForannotation")
if annotation is MKUserLocation {
//return nil
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
//println("Pinview was nil")
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
}
let button = UIButton(type: UIButtonType.detailDisclosure) as UIButton // button with info sign in it
pinView?.rightCalloutAccessoryView = button
return pinView
}
I'd like for the segue to happen when the annotation is clicked or double clicked, and then perform a segue to that new view controller while sending the specific username info as well. I'm pretty sure I'll be able to send the user info but I just need to figure out how to make the segue happen. I think it may have to do with the fact that in the new XCode update my calloutaccessory hasn't been showing. Here's a screenshot from my app. MapView Annotation
How about using didSelect method instead of calloutAccessoryControlTapped?
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
performSegue(withIdentifier: "goToUserProfile", sender: view)
print(view.annotation?.title) // annotation's title
print(view.annotation?.subtitle) // annotation's subttitle
}

Select MKMarkerAnnotationView with custom callout after adding annotation

I have an MKMapView on which I would like to add annotations on a long press gesture. After adding the annotation, I would like to select the annotation view. Quite a simple request I assumed.
The problem is that I am using the new MKMarkerAnnotationView with a custom rightCalloutAccessoryView and a custom detailCalloutAccessoryView. It is not very well documented yet, but the WWDC 2017 Session 237 states that the callout will be displayed when there is more than a title/subtitle.
Unfortunately, this is not the case for me. When I programmatically (and manually) select the annotation, I get a weird double selected state where I can see the callout AND the marker:
Here is the annotation view code:
import Foundation
import MapKit
#available(iOS 11.0, *)
protocol TemporaryUserAnnotationViewDelegate: class {
func temporaryUserAnnotationViewDidTapOk(title: String?, userAnnotationView: TemporaryUserAnnotationView)
}
#available(iOS 11.0, *)
class TemporaryUserAnnotationView: MKMarkerAnnotationView {
weak var delegate: TemporaryUserAnnotationViewDelegate?
var textField: UITextField!
init(annotation: TemporaryUserAnnotation) {
super.init(annotation: annotation, reuseIdentifier: TemporaryUserAnnotationMarkerAnnotationView.reuseIdentifier)
markerTintColor = .lbc_blue
canShowCallout = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(annotation: TemporaryUserAnnotation) {
let detailView = UIView()
detailView.translatesAutoresizingMaskIntoConstraints = false
textField = UITextField()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Titre"
detailView.addSubview(textField)
detailView.heightAnchor.constraint(equalToConstant: 21).isActive = true
detailView.widthAnchor.constraint(equalToConstant: 100).isActive = true
textField.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true
textField.centerYAnchor.constraint(equalTo: detailView.centerYAnchor).isActive = true
textField.heightAnchor.constraint(equalToConstant: 21).isActive = true
textField.widthAnchor.constraint(equalToConstant: 100).isActive = true
detailCalloutAccessoryView = detailView
let rightView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 70)))
rightView.backgroundColor = .lbc_blue
let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 40)))
button.setTitle("OK", for: .normal)
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(tapRightCalloutAccessoryViewButton), for: .touchUpInside)
rightView.addSubview(button)
rightCalloutAccessoryView = rightView
}
#objc func tapRightCalloutAccessoryViewButton() {
delegate?.temporaryUserAnnotationViewDidTapOk(title: textField.text, userAnnotationView: self)
}
}
and here is the controller code:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
print(#function)
if annotation is MKUserLocation { return nil }
if #available(iOS 11.0, *) {
switch annotation {
case let temporarayUserAnnotation as TemporaryUserAnnotation:
if let view = mapView.dequeue() as? TemporaryUserAnnotationView {
view.annotation = annotation
return view
} else {
let view = TemporaryUserAnnotationView(annotation: temporarayUserAnnotation)
view.delegate = self
view.configure(annotation: temporarayUserAnnotation)
return view
}
default:
return nil
}
} else {
let identifier = "marker"
if let view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
view.annotation = annotation
return view
} else {
return MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
}
}
}
I simply add annotations this way:
func addUserAnnotation(coordinate: CLLocationCoordinate2D) {
let annotation = TemporaryUserAnnotation(coordinate: coordinate)
mapView.addAnnotation(annotation)
mapView.selectAnnotation(annotation, animated: true)
}
I also tried to implement the mapView(_:didAdd:) method but the result is even worse with the marker view in front of the callout.
Any help would be very appreciated! Thanks!
I found the following workaround: when you add your annotation, wait a second before programmatically selecting it:
func addUserAnnotation(coordinate: CLLocationCoordinate2D) {
let annotation = TemporaryUserAnnotation(coordinate: coordinate)
mapView.addAnnotation(annotation)
DispatchQueue.main.asyncAfter(wallDeadline: .now() + .seconds(1)) {
self.mapView.selectAnnotation(annotation, animated: true)
}
}
I still think this should not be necessary, so if you have a better solution, feel free to post it here!
I just encountered a similar problem because I had a UITapGestureRecognizer of my own competing with MKMapView's selection gesture recognizer. I created and selected the annotation on the tap event, but because both gesture recognizers were firing, and theirs was firing last, it was deselecting immediately.
My solution was to implement UIGestureRecognizerDelegate and become the delegate of my gesture recognizer, and implement the following:
public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}

How to change default background color of callout bubble with detailCalloutAccessoryView

In my app I have the following sutuation.
I've implemented a custom callout bubble with custom detailCalloutAccessoryView with two labels inside.
I know how to change the color of detailCalloutAccessoryView with this line.
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
But I can't figure out how to change background color of the main bubble (it is transparent grey/white now). With view.detailCalloutAccessoryView?.backgroundColor = UIColor.red line my calloutbubble looks like this:
But I want my custom bubble to look like this:
Here is my viewFor annotation method:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let identifier = "pin"
var view : MKAnnotationView
if let dequedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) {
dequedView.annotation = annotation
view = dequedView
} else {
view = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
}
let pinImage = UIImage.init(named: "customPin")
DispatchQueue.main.async(execute: {
view.detailCalloutAccessoryView?.backgroundColor = UIColor.red
})
view.image = pinImage
configureDetailView(annotationView: view)
return view
}
I'm working in Xcode 8 w/ Swift 3.
It would be also interesting to know how to change font type and default black color of the title from black to another color.
In detail view i can easily change color of my custom labels in xib file but don't know how to access default title properties.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
//design your custom view and set it to detailCalloutAccessoryView
view.detailCalloutAccessoryView = yourDetailView
detail.superview?.superview?.backgroundColor = yourColor
// This view is type of MKSmallCalloutView
}
UIViewCallout is a private class. If you want custom callout view:
disable standart callout view.canShowCallout = false
implement MKMapViewDelegate methods with your custom UIView for callout:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let redCalloutView = RedCalloutView(view.annotation)
view.addSubview(redCalloutView)
}
func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
view.subviews.forEach {
if $0 is RedCalloutView {
$0.removeFromSuperview()
}
}
}
I had created the code for your requirement please find the below url for download the code and review it.
Link : https://www.dropbox.com/s/o2howwqceq8rsgu/MapInformation.zip?dl=0
Environment : Xcode 8 and Swift3
Highlight the code which I had done it.
I had taken the approach to display the Popup(UIPresentationController) instead of callout. For more information please find the below code.
A) I had used the UIButton to display as annotation on the MapView and display the popup when user click on it.
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let identifier = "pin"
var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as! AnnotationView?
if annotationView == nil {
annotationView = AnnotationView(annotation: annotation, reuseIdentifier: identifier)
annotationView?.canShowCallout = false
}
else {
annotationView?.annotation = annotation
}
//Take the UIButton and implement the touchupinside action for showing the popup.
let pinImage = UIImage.init(named: "customPin")
annotationView?.frame = CGRect(x: 0, y: 0, width: (pinImage?.size.width)!, height: (pinImage?.size.width)!)
annotationView?.mapPin = UIButton(frame: (annotationView?.frame)!);
annotationView?.mapPin.addTarget(self, action: #selector(ViewController.showPopup(sender:)), for: .touchUpInside)
annotationView?.addSubview((annotationView?.mapPin)!)
annotationView?.mapPin.setImage(pinImage, for: .normal)
return annotationView
}
B) Display the popup when user click on the annotation.
func showPopup(sender: UIButton!) {
let popupVC = self.storyboard?.instantiateViewController(withIdentifier: "Popup") as? Popup
popupVC?.preferredContentSize = CGSize(width: 250, height: 150)
popupVC?.modalPresentationStyle = UIModalPresentationStyle.popover
let rect = sender.superview?.convert(sender.frame, to: self.view)
popupVC?.popoverPresentationController?.delegate = self;
popupVC?.popoverPresentationController?.sourceView = self.view
popupVC?.popoverPresentationController?.sourceRect = rect!
popupVC?.popoverPresentationController?.backgroundColor = UIColor.red
self.present(popupVC!, animated: true, completion: nil)
}
Note
If you want to change the popup color from red to other different
color then you can do only single line of coding by changing the color name.
popupVC?.popoverPresentationController?.backgroundColor = UIColor.red
Please look into the below screenshot.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for v in view.subviews{
if v.subviews.count > 0{
v.subviews[0].backgroundColor = UIColor.red
}
}
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for v in view.subviews{
if v.subviews.count > 0 {
let colloutView = v.subviews[0]
colloutView.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.8)
if colloutView.subviews.count > 0 {
if colloutView.subviews[0].subviews.count > 0{
colloutView.subviews[0].subviews.forEach { (view) in
if let label = view as? UILabel{
label.textColor = UIColor.white
}
}
}
}
}
}
}