据我所知,您自定义用户跟踪按钮的能力有限。你可以改变颜色。为了让按钮离开导航栏,您必须将 UIToolbar 添加到 mapView,设置工具栏的框架大小,使其显示在您的首选位置,然后将 mkusertrackingbutton 作为项目添加到工具栏,具有灵活的空间根据需要添加以正确居中按钮。
这是我实现它的方式(到目前为止):
func setupUserTrackingButton() {
let trackingButton: MKUserTrackingBarButtonItem = MKUserTrackingBarButtonItem.init(mapView: mapView)
trackingButton.customView?.tintColor = UIColor.black
trackingButton.style = UIBarButtonItemStyle.plain
trackingButton.customView?.size = CGSize(width: 50, height: 50)
let originPoint: CGPoint = CGPoint(x: mapView.width-70,y: mapView.height-85)
let lighterGreyColor: UIColor = UIColor(colorLiteralRed: 125, green: 125, blue: 125, alpha: 1.0)
//need to implement this rounded view in order to get the box to look consistent with the mkusertracking button API
let roundedSquare: UIView = UIView(frame: CGRect(origin: originPoint, size: CGSize(width: 50, height: 50)))
roundedSquare.backgroundColor = lighterGreyColor
roundedSquare.layer.cornerRadius = 5
roundedSquare.layer.masksToBounds = true
let toolBarFrame = CGRect(origin: CGPoint(x: 3, y: 3) , size: CGSize(width: 44, height: 44))
//You may have to subclass toolbar in order to get the right coloring
let toolbar = UIToolbar.init(frame: toolBarFrame)
let flex: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
toolbar.items = [flex, trackingButton, flex]
roundedSquare.addSubview(toolbar)
mapView.addSubview(roundedSquare)
}
否则,您可能必须对按钮进行子类化并基本上构建其他功能,包括核心图形动画,这将是一些工作。希望这可以帮助。
编辑:
从 iOS 11.0+ 开始,现在有一个 MKUserTrackingButton,它是 UIView 的子类。因此,如果您在 iOS 11 下不需要任何东西,只需使用按钮和子类或自定义您想要的一切。
MKUserTrackingButton 的 Apple 文档