iOS MKMapView自定义图像显示在左上角 [英] iOS MKMapView custom images display on top left corner
本文介绍了iOS MKMapView自定义图像显示在左上角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用iOS MKMapView在地图上将图像显示为标记图标.问题是图像始终显示在屏幕的左上角.如果我触摸屏幕或稍微移动地图,图像将在正确的gps位置正确显示. 如果我不使用自定义图像,则图钉/标记会正确显示在正确的位置.
I use iOS MKMapView to show images as marker icons on the map. The problem is the images are always displayed on the top left corner of the screen. If I touch the screen or move the map a little bit, the images show correctly at the right gps locations. If I don't use custom images, the pins/markers are displayed correcly at the right location.
下面是代码:
func addAnnotationsToMap()
{
self.mapView.removeAnnotations(self.mapView.annotations)
for post in self.posts
{
let gps = CLLocationCoordinate2D(latitude: post.GPS!.latitude!, longitude: post.GPS!.longitude!)
let postPin = PostAnnotation(gps: gps)
self.mapView.addAnnotation(postPin)
}
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?
{
if (annotation is MKUserLocation)
{
return nil
}
let postAnnotation = annotation as! PostAnnotation
let reuseId = "customAnnotationView"
var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
if anView == nil
{
anView = createCustomIconView(postAnnotation)
}
else
{
anView!.annotation = annotation
}
return anView
}
func createCustomIconView(postAnnotation:PostAnnotation) -> CustomAnnotationView
{
let w = (UIScreen.mainScreen().bounds.size.width) / 8
let h = w
let customIconView = CustomAnnotationView(frame: CGRectMake(0, 0, w, h))
customIconView.imageView.image = UIImage(imageLiteral: postAnnotation.picName!)
customIconView.annotation = postAnnotation
return customIconView
}
推荐答案
信不信由你,我删除view.translatesAutoresizingMaskIntoConstraints = false
Believe it or not, the problem is fixed after I delete view.translatesAutoresizingMaskIntoConstraints = false
此答案的灵感来自 MKViewMap自定义注释在点击时消失了
这篇关于iOS MKMapView自定义图像显示在左上角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文