简体   繁体   中英

Changing Color of the Navigation Bar's Safe Area Swift UIKit

I am running into an issue. I want the whole navigation bar (including safe area) color to change but the safe area color doesnt change at all (no matter what changes I make to it).

Heres

navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.backgroundColor = .green
    
navigationItem.titleView = searchBar

I even tried changing the Nav Bar's:

  • barTintColor
  • tintColor

with no luck.

Current Navigation Bar Color

在此处输入图像描述

This view controller is being presented from the Scene Delegate using a navigation controller.

Let me know if you need any additional information.

Try this in your SceneDelegate

if #available(iOS 15, *) {
        let navigationBarAppearance = UINavigationBarAppearance()
        navigationBarAppearance.configureWithOpaqueBackground()
        navigationBarAppearance.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor : UIColor.white
        ]
       
        navigationBarAppearance.shadowColor = nil
        navigationBarAppearance.backgroundColor = .green
        UINavigationBar.appearance().barStyle = .green
        UINavigationBar.appearance().standardAppearance = navigationBarAppearance
        UINavigationBar.appearance().compactAppearance = navigationBarAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
        
        
    }

Found a solution:


let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .green
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
            
navigationItem.titleView = searchBar

I entered this in the function where I was configuring the navigation bar

Credit: https://developer.apple.com/forums/thread/682420

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM