Button inside custom Callout View not responding (Swift)

Discussion in 'iOS Programming' started by jvpython, Jul 5, 2014.

  1. jvpython macrumors 6502

    Joined:
    Aug 25, 2011
    Location:
    New Zealand
    #1
    I have a custom MKPinAnnotationView created as so:

    Code:
    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
        if annotation is MKUserLocation {
            return nil
        }
    
        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = false
            pinView!.animatesDrop = true
            pinView!.pinColor = .Red
            pinView!.draggable = true;
        } else {
            pinView!.annotation = annotation
        }
    
        return pinView
    }
    Then I add a subview to it later with a button like this:

    Code:
    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
        view.addSubview(confirm)
    }
    The problem is that the button inside the subview is not responsive at all. What could be causing that? I already tried playing around with userInteraction enabled. Thanks
     
  2. ArtOfWarfare macrumors G3

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    You've shared none of the code that creates the button so we can't help. You just add it as a subview.
     
  3. jvpython thread starter macrumors 6502

    Joined:
    Aug 25, 2011
    Location:
    New Zealand
    #3
    This is the subview code that creates the view + button:

    Code:
    let confirm = UIView(frame: CGRect(x: -30, y: -40, width: 205, height: 32))
    confirm.backgroundColor = UIColor(white: 0.9, alpha: 1)
    confirm.alpha = 0.7
    		
    let label = UILabel(frame: CGRectMake(9, 6, 150, 21))
    label.text = "Go to this location?"
    confirm.addSubview(label)
    		
    let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
    button.frame = CGRectMake(167, 2, 30, 30)
    button.setTitle("Yes", forState: UIControlState.Normal)
    button.addTarget(self, action: "confirmPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    confirm.addSubview(button)
     

Share This Page