0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Swift】NavigationBarの色が指定した色と違う(薄くなる)

Posted at

ナビゲーションバーの色を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.

AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UINavigationBar.appearance().isTranslucent = false
    return true
}

isTranslucent=falseにするとUISearchControllerが影響をうける

NavigationBar.isTranslucentfalseを設定すると、ナビゲーションバーに設定したUISearchControllerにフォーカスした際に検索バーが下にズレる現象が起きます。
その際は以下の実装をControllerに記載すれば解決します。

Controller.swift
extendedLayoutIncludesOpaqueBars = true
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?