ナビゲーションバーの色をnavigationController?.navigationBar.barTintColor
で設定した際に、設定した色と違う色になって困りました。
問題
完成イメージ
ナビゲーションの色をメインの背景の色と同様にデザインしました。
しかしnavigationController?.navigationBar.barTintColor
にメインの背景と同じ色を設定したところ、実際に表示されたナビゲーションは少し薄い色になってしまっていました。
問題のイメージ

解決
NavigationBar.isTranslucentが原因でした。
NavigationBar.isTranslucent
はナビゲーションバーの透明度を指定する値です。ナビゲーションバーに画像を設定していない場合はデフォルトはtrue
が設定されており半透明になっています。半透明にしたくない場合はこの値をfalse
にすれば解決です。
抜粋
If the navigation bar doesn't have a custom background image, or if any pixel of the background image has an alpha value of less than 1.0, the default value of this property is true. If the background image is completely opaque, the default value of this property is false. If you set this property to true and the custom background image is completely opaque, UIKit applies a system-defined opacity of less than 1.0 to the image. If you set this property to false and the background image is not opaque, UIKit adds an opaque backdrop.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UINavigationBar.appearance().isTranslucent = false
return true
}
isTranslucent=falseにするとUISearchControllerが影響をうける
NavigationBar.isTranslucent
にfalse
を設定すると、ナビゲーションバーに設定したUISearchController
にフォーカスした際に検索バーが下にズレる現象が起きます。
その際は以下の実装をControllerに記載すれば解決します。
extendedLayoutIncludesOpaqueBars = true