【问题标题】:How to add a button in front of a GMSMapView如何在 GMSMapView 前面添加一个按钮
【发布时间】:2018-10-05 12:16:00
【问题描述】:

我正在尝试在 GMSMapView 视图前添加一个按钮。 这是我的代码:

@IBOutlet weak var testButton: UIButton! 
@IBOutlet weak var mapView2: GMSMapView! 
override func viewDidLoad() {
    super.viewDidLoad()
    loadView()
}

那么这里是loadView() 方法

override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: 54.2785691, longitude: -8.4620978, zoom: 18.0)
    let mapView2 = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
    view = mapView2 
    mapView2.bringSubviewToFront(testButton)
}

这是我得到的错误:

线程 1:致命错误:在展开可选值时意外发现 nil

任何帮助将不胜感激!

【问题讨论】:

  • 请添加更多上下文,在您创建和添加 testButton 的位置,发布所有相关代码
  • @Sh_Khan 上下文足够了吗?我是新手

标签: ios swift google-maps gmsmapview


【解决方案1】:

你可以试试

class MyVc:UIViewController { 
  @IBOutlet weak var testButton: UIButton! 
  @IBOutlet weak var mapView2: GMSMapView! 
  override func viewDidLoad() {
    super.viewDidLoad()
    let camera = GMSCameraPosition.camera(withLatitude: 54.2785691, longitude: -8.4620978, zoom: 18.0)
    mapView2.animate(to: camera)
    view.bringSubviewToFront(testButton)
  }
}

我希望在 IB 中使用 UI

View-> 
    -testButton
    -mapView2

顺便说一句,您可以将视图重新排列为而不是 bringSubviewToFront

View-> 
    -mapView2
    -testButton

底部元素在前面的地方

也不要针对此类问题实施loadView,因为它仅在您以编程方式创建vc或想要在IB之外添加一些项目时才重要,在后一种情况下您应该调用super.loadView()

【讨论】:

    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多