【发布时间】: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)
}
【问题讨论】:
-
你的注释有标题吗?
-
现在不行,为什么?
-
如何设置一个?