3

I am using Mapbox SDK (SWIFT) and created my custom map. I added a button and image on the map in storyboard but when I run the application it seems like it is hidden behind. I tried everything including view.bringSubview(toFront:UIimage) and I can't use this function with a UIButton. I even tried sending the map to the backView but it messes up my custom map and still don't see my image or button. Please help! Thank you.

Arnab
  • 4,216
  • 2
  • 28
  • 50
marcoder
  • 31
  • 2
  • Share your code, would be helpful in understanding and resolving your issue – Satish Nov 23 '18 at 05:29
  • @marcoder - When you added the button and image to the mapView, did you ensure that they are child views of the mapView? i.e. Are they indented to the right of the mapView in the outline view in InterfaceBuilder? – Magnas Nov 23 '18 at 11:22

4 Answers4

2

You can also control the visibility of subviews by using:

mapview.layer.zPosition = 1
button.layer.zPosition = 2
image.layer.zPosition = 2

You can click on the debug navigator -> View UI hierarchy to visually understand how to view is being layered.

debug navigator

Alex Bailey
  • 793
  • 9
  • 20
1

You need to bring it to top of the view

Self.mapview.bringSubview(button)
Anil Kumar
  • 1,830
  • 15
  • 24
1

Please check below image make sure your Views hierarchy as followed your button should be last in hierarchy

enter image description here

Here is my output

enter image description here

or if you have button inside view then you can set it to front as

Self.view.bringSubviewToFront(button) or button.superview?.bringSubviewToFront(button)

Make sure you button is not hidden in Storyboard and when your app is running then Capture user Interface and check layers of your Storyboard.

Khush
  • 181
  • 1
  • 16
1

I couldn't change my view hierarchy's for my mapview, so I took a different approach.

I had my mapview created in my viewDidLoad function with let mapView = MGLMapView(...), my mapview was then made visible with the code view.addSubview(mapView). I had to make my button visible similarly.

First I inserted an IBOutlet into my ViewController class

@IBOutlet weak var MyButton: UIButton!

Which referenced the button I placed over my mapView in storyboard. I then added my button to the view by adding

view.addSubview(MyButton)

Right after adding my mapView.

Sam
  • 1,765
  • 11
  • 82
  • 176