如何在我的 google 地图上添加标签栏?
how do I add a tabbar on top of my google map?
我想在我的 google 地图上添加一个标签栏,但是,每当我 运行 这段代码 xcode 总是崩溃并告诉我 "fatal error: unexpectedly found nil while unwrapping an Optional value"
这是我的代码:
@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
}
我很确定错误来自 view.insertSubview(tabControlller, aboveSubview: mapView) 但是,我不确定如何在我的 google 地图上添加该选项卡?我是以编程方式还是通过 IB 来做?
感谢您的帮助!
您无法在 iOS 中将 UITabBar 设置在顶部。您可以使用任何自定义控件。
看到这个
您收到的错误是因为解包了一个包含 nil 的可选变量。
在您的代码中 tabControlller
是可选的。
当您使用它时:
view.insertSubview(tabControlller, aboveSubview: mapView)
它正在尝试打开 tabControlller
并使用它。
但是直到此时 tabControlller
可能还没有被初始化为任何对象,所以它包含 nil。所以当解包 nil 时,应用程序崩溃了。
这就是你得到 fatal error: unexpectedly found nil while unwrapping an Optional value
的原因
另外UITabBar
不能添加到控制器的顶部。相反,您可以使用 UISegmentedControl
或 UIToolBar
.
创建自定义控件
除了@PGDev 的回答之外,为了访问一个可选的值(如果它有的话),你需要打开它。可选值可以安全或强制解包。如果你强制解包一个可选的,并且它没有值,你的程序将崩溃并显示上面的消息。 你永远不应该使用 !
运算符明确地强制解包一个可选的。在某些情况下,使用 !
是可以接受的——但只有在您 100% 确定可选项包含一个值时才应该使用它。
此外,UITabBar always appear across the bottom edge of the screen and display the contents of one or more UITabBarItem 个对象。但是我找到了一些相关的答案,这些答案可能有助于如何将 UITabBar
置于顶部。请参阅下面的链接。
- UITabBar on top or any other?
希望对您有所帮助!
我想在我的 google 地图上添加一个标签栏,但是,每当我 运行 这段代码 xcode 总是崩溃并告诉我 "fatal error: unexpectedly found nil while unwrapping an Optional value"
这是我的代码:
@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
}
我很确定错误来自 view.insertSubview(tabControlller, aboveSubview: mapView) 但是,我不确定如何在我的 google 地图上添加该选项卡?我是以编程方式还是通过 IB 来做?
感谢您的帮助!
您无法在 iOS 中将 UITabBar 设置在顶部。您可以使用任何自定义控件。
看到这个
您收到的错误是因为解包了一个包含 nil 的可选变量。
在您的代码中 tabControlller
是可选的。
当您使用它时:
view.insertSubview(tabControlller, aboveSubview: mapView)
它正在尝试打开 tabControlller
并使用它。
但是直到此时 tabControlller
可能还没有被初始化为任何对象,所以它包含 nil。所以当解包 nil 时,应用程序崩溃了。
这就是你得到 fatal error: unexpectedly found nil while unwrapping an Optional value
另外UITabBar
不能添加到控制器的顶部。相反,您可以使用 UISegmentedControl
或 UIToolBar
.
除了@PGDev 的回答之外,为了访问一个可选的值(如果它有的话),你需要打开它。可选值可以安全或强制解包。如果你强制解包一个可选的,并且它没有值,你的程序将崩溃并显示上面的消息。 !
运算符明确地强制解包一个可选的。在某些情况下,使用 !
是可以接受的——但只有在您 100% 确定可选项包含一个值时才应该使用它。
此外,UITabBar always appear across the bottom edge of the screen and display the contents of one or more UITabBarItem 个对象。但是我找到了一些相关的答案,这些答案可能有助于如何将 UITabBar
置于顶部。请参阅下面的链接。
- UITabBar on top or any other?
希望对您有所帮助!