Claus
Claus

Reputation: 5742

Integrating UITableView inside iOS callout annotation

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?

Upvotes: 2

Views: 713

Answers (1)

Siddhartha
Siddhartha

Reputation: 4464

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.

Upvotes: 2

Related Questions