【问题标题】:Custom Callout view does not want to appear自定义标注视图不想出现
【发布时间】:2016-02-17 17:49:31
【问题描述】:

最终编辑我几乎重建了我所有的类(自定义 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

}

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

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 个标签和一个按钮。

我做错了什么?我已经尝试了所有的东西......仍然无法让它工作。

编辑RentalAnnotation代码

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
    }
}

EDIT 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)
    }

【问题讨论】:

  • 你的注释有标题吗?
  • 现在不行,为什么?
  • 如何设置一个?

标签: ios swift mapkit


【解决方案1】:

尝试在注释中添加title。如果 titlenil,则不会显示标注。
来自文档:

SWIFT
var canShowCallout: 布尔
目标-C
@property(nonatomic) BOOL canShowCallout

如果此属性的值为 YES,则标准标注气泡是 当用户点击选定的注释视图时显示。标注使用 来自关联注释对象的标题和副标题文本。如果 但是,没有标题文本,注释视图被视为 它的 enabled 属性设置为 NO。标注还显示任何 存储在 leftCalloutAccessoryView 中的自定义标注视图和 rightCalloutAccessoryView 属性。

【讨论】:

  • 我不断收到错误消息,告诉我 title 属性是 get-only。我该如何解决?我正在从实现 MKAnnotation 的自定义类中访问“title”属性...
  • 您可以将其添加到实现 MKAnnotation 以返回标题的自定义类中: var title: String? { get { return "你的头衔" } }
  • 我已经有了这个:var title: String? {返回出租名}
  • 能否请您发布实现 MKAnnotation 协议的类的代码?
  • 刚刚...我真的,真的不明白为什么我仍然收到错误消息...(我发布了没有您建议的更改的代码,因为即使进行了更改,它不起作用)
猜你喜欢
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多