2

My app shows a map with some pin points. I would like to visualise a tableview inside a callout bubble when one of the pin is touched. I have found something similar here but it's in Objective-C (I would like a pure Swift approach) and it requires to disable auto-layout for the custom view (which I would prefer to avoid).

enter image description here

How can I implement this solution or where I can find a library which does it?

Claus
  • 5,662
  • 10
  • 77
  • 118

1 Answers1

2

Here's one way:

Have your main controller implement UIPopoverPresentationControllerDelegate, and override its method:

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
    return .None
}

Setup the tap gesture recognizer in viewDidLoad():

let tapGesture = UITapGestureRecognizer(target: self, action: Selector("tappedOnMap:"))
yourView.userInteractionEnabled = true
yourView.numberOfTapsRequired = 1
yourView.addGestureRecognizer(tapGesture)

Define "tappedOnMap" to pop open a popover:

func tappedOnMap(sender: UITapGestureRecognizer) {
    let location = sender.locationInView(self.view)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)        
    let vc = storyboard.instantiateViewControllerWithIdentifier("tableViewControllerClass") as! tableViewControllerClass
    vc.modalPresentationStyle = .Popover
    vc.preferredContentSize = CGSizeMake(200, 150)

    if let popoverController = vc.popoverPresentationController {
        // Create popover at tapped point.
        popoverController.delegate = self
        popoverController.sourceRect = CGRectMake(location.x, location.y, 20, 10)
        popoverController.sourceView = self.view
        self.presentViewController(vc, animated: true, completion: nil)
    }
}

Define "tableViewControllerClass" to have your table view, and attach it to a scene in storyboard. Hope that helps.

Siddhartha
  • 4,296
  • 6
  • 44
  • 65
  • You're welcome. I'm working on a popover controller myself (http://i.imgur.com/89REGEb.png) but the one you provided looks far better than mine. Going to give it a greyish translucent makeover. – Siddhartha Jan 03 '16 at 22:02
  • Will this work well when a zoom or pan is performed on the map? – Claus Jan 04 '16 at 15:43
  • That is a very good question. I just tried it on mine and it seems to work fine. If you hold ```Alt``` and drag one of the 2 grey buttons that appear, it'll zoom in. You can't zoom/pan when the popover is popped up. – Siddhartha Jan 04 '16 at 18:47