ios - map 注释不会显示 rightCalloutAccessoryView 的自定义 UIButton

标签 ios swift

我有一张带有数十个注释的 map 。注释正确显示标题和副标题,我想在它们的右侧添加一个自定义“Go”按钮。 “前往”按钮将打开 Apple map ,并向用户指示他们在 map 上查看的注释。

我复制了下面我正在使用的代码。在当前的形式中,不会出现任何按钮。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is MKUserLocation) {
        let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))

        let rightButton = UIButton(type: .custom)
        rightButton.setImage(UIImage(named: "Go"), for: .normal)
        rightButton.tag = annotation.hash

        pinView.canShowCallout = true
        pinView.rightCalloutAccessoryView = rightButton

        return pinView
    }
    else {
        return nil
    }
}

如果我删除以下行:

rightButton.setImage(UIImage(named: "Go"), for: .normal)

并将其上面的更改为:

let rightButton = UIButton(type: .detailDisclosure)

按钮确实正确显示,这告诉我,我尝试为按钮使用自定义图像时出现问题。

对我缺少的东西有什么想法吗?

最佳答案

我能够通过将 UIButton 类型更改为以下内容来纠正它:

let rightButton = UIButton(type: .infoLight)

我的代码片段现在看起来是这样的:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is MKUserLocation) {
        let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: String(annotation.hash))

        let rightButton = UIButton(type: .infoLight)
        rightButton.setImage(UIImage(named: "Go"), for: .normal)
        rightButton.tag = annotation.hash

        pinView.canShowCallout = true
        pinView.rightCalloutAccessoryView = rightButton

        return pinView
    }
    else {
        return nil
    }
}

关于ios - map 注释不会显示 rightCalloutAccessoryView 的自定义 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41686875/

相关文章:

ios - UIAccessibilityContainer 动态更新协议(protocol)方法

ios - 如何在 View Controller 之间正确进行分隔

ios - 从 View Did Load 中检索到的数据创建 TableView Controller

ios - OpenGL 支持的 UIView 中的旁白和滚动

swift - xcode 8 测试版 3 : Expected ',' joining parts of a multi-clause condition

ios - 何时在 Swift 中使用泛型

swift - 使用 AVAudioSourceNode 播放声音时的噪音

执行命令时 Swift 脚本卡住 (ffmpeg)

ios - swift Firebase 删除观察者

Swift OSX 第一个运行的方法是什么?