Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating white-translucent UINavigationBar

Tags:

ios

swift

Typically, translucent UINavigationBars have a light gray color above a white background.

Image 1

However, many navigation bars throughout iOS 11 have a white color. For example, navigation bars in the Files app are white AND translucent which are noticeably different from setting barTintColor to white.

Image 2 Image 3

How do I achieve this kind of effect on a UINavigationBar?

like image 839
Affinity Avatar asked Nov 08 '22 12:11

Affinity


1 Answers

  1. Set the barTintColor of the navigation bar to white.
  2. After that, subclass UINavigationBar and set the shadow image to an empty UIImage.

    class CustomNavBar: UINavigationBar {
       override func awakeFromNib() {
          super.awakeFromNib()
          shadowImage = UIImage()
       }
    }
    
  3. Finally, set the class of the navigation bar to the custom navigation bar class you just created.

Result

translucent navigation bar, white background

translucent navigation bar, partially black background

like image 51
Tamás Sengel Avatar answered Nov 15 '22 11:11

Tamás Sengel