iOS迅速将我的位置按钮移动到右上角

时间:2016-09-21 06:39:09

标签: ios swift user-interface location

目前,当我使用GMSMapView api设置按钮时,它出现在右下角。

还有另外一个关于此的问题,但这是针对swift而不是objective-c(并且它适用于屏幕上的不同位置。)

3 个答案:

答案 0 :(得分:2)

只需调整此post

中的解决方案即可

从头顶看,它看起来像这样:

let myLocationButton = mapView.subviews.last! as! UIButton
myLocationButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin
let frame = myLocationButton.frame
frame.origin.y = 10
myLocationButton.frame = frame

答案 1 :(得分:1)

创建您自己的myLocationButton,只需点按即可将相机目标更新为当前位置。

答案 2 :(得分:0)

这里是完整的实现。经过测试:

class MyViewController : UIViewController {

    var userTracker : MKUserTrackingButton? //strong reference
    var compass : MKCompassButton?    //strong reference

    override func viewLayoutMarginsDidChange() { //when device is rotated
        setUpUserLocationButton()
    }

    func setUpUserLocationButton() {
        let buttonMargins : CGFloat = 12
        if userTracker == nil {
            userTracker  = MKUserTrackingButton(mapView: self.mapView)
            userTracker!.backgroundColor = .white
            view.addSubview(userTracker!)
            userTracker!.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin]
            setGenericShadow(subView: userTracker!)

            self.mapView.showsCompass = false
            compass = MKCompassButton(mapView: mapView)
            compass!.compassVisibility = .visible
            compass!.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin]
            view.addSubview(compass!)
        }

        compass!.frame.origin.x = (self.view.frame.width) - (compass!.frame.size.width) - (self.view.safeAreaInsets.right == 0 ? buttonMargins : self.view.safeAreaInsets.right )
        compass!.frame.origin.y = self.view.safeAreaInsets.top + buttonMargins

        userTracker!.frame.origin.x = (self.view.frame.width) - (userTracker!.frame.size.width) - (self.view.safeAreaInsets.right == 0 ? buttonMargins : self.view.safeAreaInsets.right )
        userTracker!.frame.origin.y = self.view.safeAreaInsets.top + buttonMargins + compass!.frame.height + buttonMargins
    }

    func setGenericShadow(subView:UIView) {
        subView.layer.shadowPath = UIBezierPath(rect:  subView.bounds).cgPath
        subView.layer.shadowOpacity = 0.2
        subView.layer.shadowColor = UIColor.black.cgColor
        subView.layer.shadowOffset = CGSize(width: 0, height: 0)
        subView.layer.shadowRadius = 4
        subView.layer.cornerRadius = 5
        subView.layer.masksToBounds = false
    }

}

enter image description here