最后的编辑我几乎重建了我的所有类(自定义MKAnnotationView,自定义标注视图以及另一个支持MKAnnotation协议的自定义类(这是我的标注类)),它只是开始工作并显示标注。我在测试项目中将所有内容归结为基本要点(即仅使用自定义注释视图而不使用自定义标注,然后重新实现自定义标注)以更清楚地看到事情,然后工作,然后移植结果回到我的实际项目。标注视图已显示,但显示不正确(我的自定义detailCalloutAccessoryView被切断,仍在尝试解决该问题)。



当我点击自定义注释视图时,我的MapKit自定义标注视图只是不希望出现怪胎。我的代码有什么问题?

这是我的“ viewForAnnotation”函数...自定义注释视图可以正常工作,只是根本没有出现标注视图。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if let annotation = annotation as? RentalAnnotation {
            let identifier = "pin"
            var customAnnotationView: CustomAnnotationView

    var customCalloutView = CustomCalloutView()
    customCalloutView.backgroundColor = UIColor.greenColor()
    let widthConstraint = NSLayoutConstraint(item: customCalloutView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 40)
    customCalloutView.addConstraint(widthConstraint)

    let heightConstraint = NSLayoutConstraint(item: customCalloutView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 20)
    customCalloutView.addConstraint(heightConstraint)

    let image = UIImageView(image: UIImage(named: "bed"))
    image.contentMode = .ScaleAspectFit
    if let dequeuedView = rentalsMapView.dequeueReusableAnnotationViewWithIdentifier(identifier) as? CustomAnnotationView {
        dequeuedView.annotation = annotation
        dequeuedView.label!.text = "$ \(annotation.price)"
        dequeuedView.canShowCallout = true
        dequeuedView.detailCalloutAccessoryView = customCalloutView
        customAnnotationView = dequeuedView
    } else {
        customAnnotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        customAnnotationView.label?.text = "$ \(annotation.price)"
        customAnnotationView.canShowCallout = true
        customAnnotationView.detailCalloutAccessoryView = customCalloutView
        customAnnotationView.calloutOffset = CGPoint(x: +5, y: 5)

    }
    return customAnnotationView
}
return nil


}

这是我的UIView的CustomCalloutView子类的代码:

class CustomCalloutView: UIView {

    @IBOutlet weak var calloutPropertyAvailabilityDate: UILabel!
    @IBOutlet weak var calloutPropertyBedroomNumber: UILabel!
    @IBOutlet weak var calloutPropertyBathroomNumber: UILabel!
    var customCalloutView = UIView()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        customCalloutView = (NSBundle.mainBundle().loadNibNamed("CustomCalloutView", owner: self, options: nil)[0] as? UIView)!
        self.addSubview(customCalloutView)
    }

}


我设计了一个带有XIB文件的自定义标注视图界面,​​该文件有6个标签和一个按钮。

我究竟做错了什么 ?我实际上已经尝试了一切...仍然无法正常工作。

出租注释的编辑代码

class RentalAnnotation: NSObject, MKAnnotation {

    var rentalName: String
    var price: Int
    var latitude: Double
    var longitude: Double
    var coordinate: CLLocationCoordinate2D

    init(rentalName: String, price: Int, latitude: Double, longitude: Double, coordinate: CLLocationCoordinate2D) {
        self.rentalName = rentalName
        self.price = price
        self.latitude = latitude
        self.longitude = longitude
        self.coordinate = coordinate
    }

    var title: String? {
        return rentalName
    }

    var subtitle: String? {
        return ("$\(price)")
    }

    func mapItem() -> MKMapItem {
        let addressDictionary = [String(CNPostalAddressStreetKey): rentalName]
        let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: addressDictionary)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = rentalName
        return mapItem
    }
}


编辑2 didSelectAnnotationView方法的代码

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

        var customView = (NSBundle.mainBundle().loadNibNamed("CustomCalloutView", owner: self, options: nil))[0] as? CustomCalloutView

//        var calloutFrame = customView?.frame
//        calloutFrame?.origin = CGPointMake(-((calloutFrame?.size.width)!/2) + 15, -((calloutFrame?.size.height)!))
//        customView?.frame = calloutFrame!

        let cpa = view.annotation as? CustomPointAnnotation
//
//        view.addSubview(customView!)

        let spanX = 0.0000000000000001
        let spanY = 0.0000000000000001

        let newRegion = MKCoordinateRegion(center: (cpa?.coordinate)!, span: MKCoordinateSpanMake(spanX, spanY))
        self.rentalsMapView.setRegion(newRegion, animated: true)
    }

最佳答案

尝试将title添加到注释中。如果titlenil,则不会显示标注。从文档中:


  迅速
  var canShowCallout:布尔
  目标C
  @property(nonatomic)BOOL canShowCallout
  
  如果此属性的值为YES,则标准标注气泡为
  当用户点击选定的注释视图时显示。标注使用
  关联注释对象的标题和字幕文本。如果
  没有标题文本,但是,注释视图被视为
  其enabled属性设置为NO。标注还会显示任何
  自定义标注视图存储在leftCalloutAccessoryView和
  rightCalloutAccessoryView属性。

关于ios - 自定义标注 View 不希望出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35464065/

10-12 13:37