Navigation SDK for iOS 不会自动遵循 iOS 系统浅色和深色模式。
相反,启用导航功能的 GMSMapView 通过其 lightingMode 属性和 GMSNavigationLightingMode 枚举支持正常和低光照配色方案:
启用自动灯光模式切换
Navigation SDK for iOS 会根据一天中的时间和设备的位置计算建议的照明模式,并通过 GMSNavigator 的 suggestedLightingMode 属性公开该模式。
不过,GMSMapView不会自动采纳此建议。
如需在普通模式和低光模式之间自动切换,请在视图控制器中完成以下步骤:
- 在启动时设置初始照明模式:设置地图时,从
GMSNavigator读取suggestedLightingMode属性,并将该值分配给mapView.lightingMode。由于监听器仅在建议的光照模式发生变化时(例如在日出或日落时)触发,因此设置初始值可确保在用户夜间开始导航时,地图立即以低光照模式呈现。 - 在导航期间监听照明模式变化:采用
GMSNavigatorListener协议并实现navigator(_:didChangeSuggestedLightingMode:)方法,以便在建议的模式发生变化时更新mapView.lightingMode。
以下示例展示了如何设置初始照明模式,并在建议的照明模式发生变化时更新该模式:
Swift
// Step 1: Set the initial lighting mode on launch or setup.
mapView.navigator?.add(self)
mapView.lightingMode = mapView.navigator?.suggestedLightingMode ?? .normal
// Step 2: Update the lighting mode when the sun sets or rises during navigation.
func navigator(
_ navigator: GMSNavigator,
didChangeSuggestedLightingMode lightingMode: GMSNavigationLightingMode
) {
mapView.lightingMode = lightingMode
}
Objective-C
// Step 1: Set the initial lighting mode on launch or setup.
[_mapView.navigator addListener:self];
_mapView.lightingMode = _mapView.navigator.suggestedLightingMode;
// Step 2: Update the lighting mode when the sun sets or rises during navigation.
- (void)navigator:(GMSNavigator *)navigator
didChangeSuggestedLightingMode:(GMSNavigationLightingMode)lightingMode {
_mapView.lightingMode = lightingMode;
}
如需详细了解如何采用 GMSNavigatorListener,请参阅“监听导航事件”指南中的声明对所需协议的一致性。
强制使用正常模式或弱光模式
如果您不希望地图自动切换模式,可以通过直接设置 mapView.lightingMode 并省略 navigator(_:didChangeSuggestedLightingMode:) 监听器更新来强制使用正常模式或弱光模式。
以下示例展示了如何强制将地图设为低光模式或正常模式:
Swift
// Force low-light mode.
mapView.lightingMode = .lowLight
// Or force normal (daylight) mode.
mapView.lightingMode = .normal
Objective-C
// Force low-light mode.
_mapView.lightingMode = GMSNavigationLightingModeLowLight;
// Or force normal (daylight) mode.
_mapView.lightingMode = GMSNavigationLightingModeNormal;
自定义弱光界面颜色
您还可以自定义地图处于低光模式时导航标题栏的主要和次要背景颜色。如需详细了解如何自定义这些颜色,请参阅“修改导航界面”指南中的低光照模式标题颜色。