ios - 调整图像大小以适应 MapView 的 leftCalloutAccessoryView

标签 ios swift mkmapview mapkit

我正在尝试调整 UIImage 的大小以适应 leftCalloutAccessoryView 插槽中 map View 的注释。我尝试(很差地)添加约束但图像没有显示 - 没有显示的约束,但它太大了。现在有了它们,它根本就没有出现,我在想,因为它已经被调整到 View 之外了。我尝试添加 leftAnchortopAnchor 但出现错误,因为我不完全确定我应该将它固定到什么 - 附件 View ?注释 View ?到目前为止,这里是代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    let identifier = "Restaurants"

    if annotation is Restaurants {
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true

            let infoButton = UIButton(type: .detailDisclosure)
            let imgView = UIImageView(image: UIImage(named: "TTT"))

            // constraints
            imgView.translatesAutoresizingMaskIntoConstraints = false
            imgView.widthAnchor.constraint(equalToConstant: 20).isActive = true
            imgView.heightAnchor.constraint(equalToConstant: 20).isActive = true

            annotationView!.rightCalloutAccessoryView = infoButton
            annotationView!.leftCalloutAccessoryView = imgView
        } else {
            annotationView!.annotation = annotation
        }
        return annotationView
    }
    return nil
}

我是否只需要添加侧面和顶部 anchor ,或者是否有完全不同的方法来解决这个问题?

谢谢!

最佳答案

您可以尝试将UIImageView大小设置为创建的MKPinAnnotationView

然后调用 aspect fit 像这样:

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: annotationView!.frame.height, height: annotationView!.frame.height))

imageView.image = UIImage(named: "TTT")
imageView.contentMode = .scaleAspectFit

annotationView!.leftCalloutAccessoryView = imageView

关于ios - 调整图像大小以适应 MapView 的 leftCalloutAccessoryView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42402232/

相关文章:

ios - UICollectionView 和在单元格上写文本

swift - 在使用 URLSession 的这个简单案例中是否创建了强引用循环?

iphone - 如何在第二次点击时取消选择 map 注释

ios - MKMapView setRegion 如何停止动画

ios - 向 map 添加多个注释但都显示一些相同的数据

ios - 遵循安全路径的数据库事务

iphone - 如何在 UIBezierPath 上获取边框

ios - 如何在 iOS 中覆盖 Cordova 的 audioCapture 接口(interface)?

ios - 位置管理器 :didEnterRegion method is not getting called when screen is off

swift - 仅当点击部分中的其他单元格时才取消选择单元格