[Swift] The color of the Navigation Bar is different (lighter) from the specified color.

When I set the color of the navigation bar with navigationController? .NavigationBar.barTintColor, it was different from the set color.

problem

Complete image

I designed the navigation color as well as the main background color. However, when I set navigationController? .NavigationBar.barTintColor to the same color as the main background, the actual navigation displayed was a little lighter.

Image of the problem

Solution

It was caused by NavigationBar.isTranslucent. NavigationBar.isTranslucent is a value that specifies the transparency of the navigation bar. If you have not set an image in the navigation bar, the default is true and it is translucent. If you don't want it to be translucent, you can set this value to false.

** Excerpt **

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
}

UISearchController is affected when isTranslucent = false

If you set false to NavigationBar.isTranslucent, the search bar will shift downward when you focus on ʻUISearchController` set in the navigation bar. In that case, you can solve it by writing the following implementation in Controller.

Controller.swift


extendedLayoutIncludesOpaqueBars = true

Recommended Posts

[Swift] The color of the Navigation Bar is different (lighter) from the specified color.
[Swift] Change the color of SCN Node
What is different from the PHP language. [Note]
[Comparison verification] How different is the development productivity of Spring Boot apps from the past?
[Swift] This is the solution! Illustration of Delegate implementation
I want to change the color of the upper control navigation bar (Control menu) in Liferay 7 / DXP
iOS app development: Timer app (9. Customize the color of the progress bar)
[Swift] Get the timing when the value of textField is changed
Learn how to customize the Navigation Bar from the sample app
[Java] Delete the specified number of characters from the end of StringBuilder
[Swift] Determine the constellation from the date of birth entered in the UIDatePicker
'% 02d' What is the percentage of% 2?
[Swift] Change the textColor of UIDatePicker
The basic basis of Swift dialogs
[Java Servlet] The road of Senri is also the fifth step from the first step
The content of the return value of executeBatch is different between 11g and 12c
[Java Servlet] The road of Senri is also the third step from the first step
From Java9, the constructor of the class corresponding to primitive types is deprecated.
[Java Servlet] The road of Senri is also the fourth step from the first step
[swift5] How to change the color of TabBar or the color of item of TabBar with code