2016-10-18 3 views
0

作成したxibでカスタムコールアウトビューを設定しようとしていますが、表示されません。xibでカスタムコールアウトビュー

私XIB LocationInfoは、私はこれは私が示すことができていないので、動作するかどうかわからない背景画像を(設定するには、私のXIBにビューのカスタムUIViewクラスを作成しました。この enter image description here

のように見えますXIB)

import Foundation 
import UIKit 
import MapKit 

class AddressView: MKPinAnnotationView{ 
    override func draw(_ rect: CGRect) { 
     super.draw(rect); 

     UIGraphicsBeginImageContext(self.frame.size) 
     UIImage(named: "Location.Info-background")?.draw(in: self.bounds) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     self.backgroundColor = UIColor(patternImage: image!) 
    } 

    override func setSelected(_ selected: Bool, animated: Bool) { 
     //todo 
    } 

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { 
     //todo 
     return nil 
    } 
} 

import Foundation 
import MapKit 
import UIKit 

class MapPin: MKPointAnnotation{ 
    var name: String 
    var street: String 
    var type: String 
    var postCode: String 

    init(name: String, street: String, type: String, postCode: String){ 
     self.name = name 
     self.street = street 
     self.type = type 
     self.postCode = postCode 
    } 
} 

を次のように私のカスタムannotationpinクラスがあると私はトンをしようとしていますo View Controllerクラスで次のようにこのすべてを使用してください。

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     CLGeocoder().reverseGeocodeLocation(manager.location!) { (placemarks, error) in 
      if (error != nil){ 
       return 
      } 
      if placemarks?.count != nil{ 
       let pm = (placemarks?[0])! as CLPlacemark 
       self.displayLocationInfo(placemark: pm) 
      } 
     } 

     let spanX = 0.00725 
     let spanY = 0.00725 
     locationManager.stopUpdatingLocation() 
     let location = locations.last! as CLLocation 
     let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) 
     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: spanX, longitudeDelta: spanY)) 
     self.MapRSR.setRegion(region, animated: true) 
     self.MapRSR.delegate = self 

     let mapPin = MapPin(name: "", street: "", type: "", postCode: "") 
     mapPin.coordinate = center 
     mapPin.title = "test" 

     self.MapRSR.addAnnotation(mapPin) 
    } 


    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     let pin = mapView.dequeueReusableAnnotationView(withIdentifier: "LocationInfo") ?? AddressView(annotation: annotation, reuseIdentifier: "LocationInfo") 
     pin.canShowCallout = true 
     return pin 
    } 

xibビューは表示されません。誰かが私が間違ってやっていることを知っているか、私が望む効果をどのように達成できるかは、このようなものです。 enter image description here

答えて

2

didSelectAnnotationViewdidSelectAnnotationViewバンドルからxibをロードし、サブビューをアノテーションビューに追加します。ここCustomXibCalloutはxibファイルでCustomCalloutViewはMKAnnotationViewです。

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) { 
    if view.annotation!.isKindOfClass(MKUserLocation){ 
     return 
    } 

    //Custom xib 
    let customView = (NSBundle.mainBundle().loadNibNamed("CustomXibCallout", owner: self, options: nil))[0] as! CustomCalloutView; 

    let calloutViewFrame = customView.frame; 

    customView.frame = CGRect(x: -calloutViewFrame.size.width/2.23, y: -calloutViewFrame.size.height-7, width: 315, height: 200) 

    view.addSubview(customView) 
} 

didDeselectAnnotationViewで添加図

func mapView(mapView: MKMapView, didDeselectAnnotationView view: MKAnnotationView) 
{ 
    for childView:AnyObject in view.subviews{ 
     childView.removeFromSuperview(); 
    } 
} 
を除去

Example for CustomCallout

+0

この部分 'せcalloutViewFrame = customView.frame。 customView.frame = CGRect(x:-calloutViewFrame.size.width/2.23、y:-calloutViewFrame.size.height-7、width:315、height:200) 'ビューが表示されないようにします。私がそれを取り出すと、それは明らかになるはずですが、明らかにそうであるようには見えません。 – NoSixties

+0

@NoSixtiesは、注釈ビューのカスタムサイズを設定するものです。私のケースでは、自分の要件のフレームサイズを使用しました。欲しいです。 – Jeyamahesan

+0

私はそれを設定しようとしたが、それは私のために動作していないようです。 – NoSixties