iOS swift将我的位置按钮移动到右上角
Posted
技术标签:
【中文标题】iOS swift将我的位置按钮移动到右上角【英文标题】:iOS swift move my location button to top right corner 【发布时间】:2016-09-21 06:39:08 【问题描述】:目前,当我使用 GMSMapView api 设置按钮显示时,它显示在右下角。
关于这个还有另一个关于 SO 的问题,但这是针对 swift 而不是objective-c(它针对屏幕上的不同位置。)
【问题讨论】:
【参考方案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
【讨论】:
以上是关于iOS swift将我的位置按钮移动到右上角的主要内容,如果未能解决你的问题,请参考以下文章
在屏幕上的 3 个特定位置生成敌人 Swift SpriteKit IOS
Swift:锁定按钮位置,因为它正在被 UITextview 移动?约束问题?