0

I am trying to add a tab bar on top of my google map however, whenever I run this code xcode always crashes and tells me "fatal error: unexpectedly found nil while unwrapping an Optional value"

Here's my code:

 @IBOutlet var tabControlller: UITabBar!

override func loadView() {
    let camera = GMSCameraPosition.cameraWithLatitude(25.0330, longitude: 121.5654, zoom: 6.0)
    let mapView = GMSMapView.mapWithFrame(CGRectMake(10, 10, 10, 10), camera: camera)
    mapView.myLocationEnabled = true
    mapView.settings.tiltGestures = true
    mapView.settings.rotateGestures = true

    view = mapView
    view.insertSubview(tabControlller, aboveSubview: mapView)


    // Creates a marker in the center of the map
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: 25.0330, longitude: 121.5654)
    marker.title = "Taipei"
    marker.snippet = "Taiwan"
    marker.map = mapView

    let mapInsets = UIEdgeInsets(top: 100.0, left: 0.0, bottom: 0.0, right: 0.0)
    mapView.padding = mapInsets


}

I am pretty sure the error comes from the view.insertSubview(tabControlller, aboveSubview: mapView) however, I am not sure how to add that tabbar on top of my google map? Do I do it programmatically or through IB?

Thanks for your help!

user3175707
  • 1,123
  • 3
  • 11
  • 14

3 Answers3

0

You can't set UITabBar on top in iOS. you can use any custom controll.

See this

https://developer.apple.com/reference/uikit/uitabbar

Piyush Sanepara
  • 1,417
  • 2
  • 18
  • 23
0

The error you are getting is because of unwrapping an optional variable that contains nil.

In your code tabControlller is an optional.

When you are using it in:

view.insertSubview(tabControlller, aboveSubview: mapView)

it is trying to unwrap tabControlller and use it. But till this point tabControlller might not be initialized to any object and so it contains nil. So when unwrapping nil , the app crashes.

That why you are getting fatal error: unexpectedly found nil while unwrapping an Optional value

Also UITabBar cannot be added to the top of the controller. Instead you can create custom control using UISegmentedControl or UIToolBar.

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

In addition to @PGDev's answer, in order to access an optional’s value (if it has one at all), you need to unwrap it. An optional value can be unwrapped safely or forcibly. If you force-unwrap an optional, and it didn't have a value, your program will crash with the above message. Source. You should never explicitly force unwrap an optional with the ! operator. There may be cases where using ! is acceptable – but you should only ever be using it if you are 100% sure that the optional contains a value.

Also, UITabBar always appear across the bottom edge of the screen and display the contents of one or more UITabBarItem objects. But I found some related answers that may help on how to position the UITabBar on top. See the links below.

Hope this helps!

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59