This is an demonstration of a custom map view annotation callouts in Swift.
The easiest way to customize callouts is to implement the leftAccessoryView
and rightAccessoryView
. If you want more considerable changes for what appears below the title in the callout (e.g. add some custom imagery or whatever), you can also implement the detailAccessoryView
.
But sometimes, even that is insufficient. For example, if you want to change the background color of the callout, or change the shape of the callout bubble, or whatever, adjusting the various accessory views is insufficient. In those cases, you can disable the standard the MapKit callout, and generate your own. You simply have to identify when the callout is selected, and then add a subview to the annotation view.
When you go down this road, though, you're signing up for the manual rendering of the callout yourself. It gives you the ultimate level of control, but takes considerably more work.
In this project, I have an abstract CalloutView
class that renders a custom callout bubble, and has a contentView
associated with it. In my concrete subclass of this, ExampleCalloutView
, I add two text labels. Clearly you can do something more substantial than that, but I wanted to do enough here so that you could see that you have fine-grained control over the appearance (in this case, changing the background color and the UIBezierPath
shape around the callout).
I have also expanded this to detect taps on the callout, namely (a) adding a hitTest
to the annotation view to included the callout, if present; and (b) added hitTest
to callout base class to detect taps within the bubble. There is now a method, didTouchUpInCallout
, that you can override if you want to take some action when the callout's contentView
is tapped.
This is not intended as an end-user library, but just a "simple" example of how one might create custom callouts. This is for illustrative purposes only.
See http://stackoverflow.com/a/30824051/1271826.
Developed in Swift on Xcode 8.1 for iOS 10 using Swift 3, but updated for Xcode 11.5 and Swift 5.2. See the swift2
branch for Swift 2 on iOS 9. But, the basic ideas are equally applicable for different versions of Swift and Objective-C.
Copyright © 2015-2020 Robert Ryan. All rights reserved.
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
--
15 June 2015