viewDidLoad()中的addSubView()和bringSubViewToFront()不起作用

时间:2016-04-14 03:06:16

标签: ios iphone swift uiviewcontroller viewdidload

我正在构建一个使用Google Maps SDK的应用程序。为此,我将视图(根视图)定义为mapView。现在我要添加另一个我希望在mapView之上的视图,但是当我在viewDidLoad()中执行它时它就不会发生。我真的很感激帮助,甚至解释为什么会发生这种情况...... topViewBar只是我在Storyboard中添加的UIView。

class MapViewController: UIViewController {

@IBOutlet weak var topViewBar: UIView!



override func viewDidLoad() {
    super.viewDidLoad()
//MARK: - put event on map function

    func putEventOnMAp(latitude: CLLocationDegrees, longitude: CLLocationDegrees, eventInfo: String, map: GMSMapView) {
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(latitude, longitude)
        marker.title = eventInfo
        marker.map = map


    }

//MARK: - Initialize map view
    let camera = GMSCameraPosition.cameraWithLatitude(-33.86,
                                                      longitude: 151.20, zoom: 6)
    let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
    mapView.myLocationEnabled = true
    self.view = mapView
    self.view.addSubview(topViewBar) // these two lines is where I try to add the additional view
    self.view.bringSubviewToFront(topViewBar)


    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
    marker.title = "Sydney"
    marker.snippet = "Australia"
    marker.map = mapView

1 个答案:

答案 0 :(得分:0)

您正在添加topViewBar作为mapView的子视图。我想您要做的是初始化一个简单的容器UIView以将self.view设置为。然后将mapViewtopViewBar都添加为UIView

的子视图