【问题标题】:iOS swift move my location button to top right corneriOS swift将我的位置按钮移动到右上角
【发布时间】:2016-09-21 06:39:08
【问题描述】:

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

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

【问题讨论】:

    标签: ios swift user-interface location


    【解决方案1】:

    只需调整这个post中的解决方案

    在我看来,它看起来像这样:

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

    【讨论】:

      【解决方案2】:

      创建您自己的 myLocationButton 并在点击时将您的相机目标更新到当前位置。

      【讨论】:

        【解决方案3】:

        这里是一个完整的实现。久经考验:

        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
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-05
          • 2017-08-24
          • 2014-11-18
          相关资源
          最近更新 更多