【问题标题】:Custom callout view with xib使用 xib 自定义标注视图
【发布时间】:2016-10-18 11:25:14
【问题描述】:

我正在尝试使用我创建的 xib 设置自定义标注视图,但它没有显示出来。

我的 xib LocationInfo 看起来像这样

我为我的 xib 中的视图创建了一个自定义 uiview 类来设置背景图像(不确定这是否有效,因为我无法显示 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
    }
}

我正在尝试在我的视图控制器类中使用这一切

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 视图。有谁知道我做错了什么或如何达到我想要的效果,就像这样。

【问题讨论】:

    标签: ios mapkit mapkitannotation


    【解决方案1】:

    didSelectAnnotationView 从bundle 中加载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

    【讨论】:

    • 这部分let calloutViewFrame = customView.frame; customView.frame = CGRect(x: -calloutViewFrame.size.width/2.23, y: -calloutViewFrame.size.height-7, width: 315, height: 200) 确保视图不显示。当我把它拿出来时,它会显示出来,但显然不像它应该显示的那样
    • 我知道我已经尝试设置它,但它似乎对我不起作用。
    • 您的示例应用程序中的“test”reuseid 来自哪里?因为当我尝试实现它时,我的 pin 消失了大声笑
    • "reuseId" 用于重用注释视图,如重用tableview行,可以使用任何静态id。该id与pin无关。我不知道你是如何实现的。请接受并投票给答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多