When we use the old way of making the navigation bar translucent or opaque using isTranslucent property on the navigation bar, the view's safeAreaInset returns some value other than 0 for the translucent navigation bar and viceVersa.
But this behaviour is not seen when using the below code as pe new iOS 13 SDK
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.systemRed
appearance.titleTextAttributes = [.foregroundColor: UIColor.lightText] // With a red background, make the title more readable.
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
navigationItem.compactAppearance = appearanc
For this code i am still seeing safeAreaInset.top > 0.
Please help me to understand this behaviour. And how to get inset.top as 0 with new UINavigationBarAppearance api.
Answering my own question:
Using isTranslusent with Appearance API helped me to achieve the old behavior.
As appearance.configureWithOpaqueBackground() only make the bar appearance opaque.
Related
Hello I am creating app with Swift i have just started creating app and take one View Controller and embed in navigation controller and for remove navigation bar border i used below code
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for:.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.layoutIfNeeded()
and for dealing with Dark Theme i have added one more line as below
i am adding Screen Shot Of Light Mode And Dark Mode for better explanation
overrideUserInterfaceStyle = .light
but when iphone is in darkmode Navigation Bar Style changed to light is there any solution to handle navigation bar style while dark mode is enabled
This dark mode Screen Shot
This Is Light Mode Screen Shot
Kindly tell me if anyone have any solution for this
Use UINavigationBarAppearance() to customize UINavigationBar in iOS13 (where dark mode feature appeared):
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor.colorYouNeed
self.navigationController?.navigationBar.standardAppearance = appearance
}
My app has many ViewControllers that are pushed on navigation stack.
I have configured UINavigationBar appearance globally in AppDelegate as below.
let appearance = UINavigationBar.appearance()
appearance.barTintColor = myColor
appearance.tintColor = .white
appearance.isTranslucent = false
let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: myFont, size: mySize)]
appearance.titleTextAttributes = textAttributes as [NSAttributedString.Key : Any]
All ViewControllers work as expected except only one ViewController.
Below is one of the VCs that works as expected. It shows color and font that I want.
And below is the ViewController that shows different look unlike others.
I can't understand the reason why only one navigationBar on this VC shows different appearance.
So I've done debugging view hierarchy.
Below is the view hierarchy of VCs that works as expected.
And below is the view hierarchy of VC that shows weird look.
As seen on the picture, the problematic NavigationBar has two more layers, UIVisualEffectView and UIVisualEffectBackdropView.
I am an experienced iOS developer and have no idea why this happens.
I carefully checked all the setting related to NavigationBar on the IB but found no difference from others.
I even removed the ViewController, embedding NavigationController completely and rebuilt them from scratch without luck.
Please somebody explain me why only this NavigationBar has different structure.
I'm working on iOS 13.3 & Xcode 11.3.1
Working here on iOS15, I tried a lot of things to customize the nav bar appearance to achieve a simple opaque color, nothing worked except this:
In viewDidLoad
if (#available(iOS 13.0, *)) {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.backgroundColor = [UIColor orangeColor];
self.navigationController.navigationBar.standardAppearance = appearance;
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
}
Swift version:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .orange
navigationBar.standardAppearance = appearance;
navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
Finally if you only want to apply this on the current controller, and restore the default nav bar just do the same but with a fresh UINavigationBarAppearance() on viewWillDisappear.
I am tried to remove the navigation bar background in iOS 13 in the if's statements with #available. I know the original code to remove the navigation bar background for iOS 12 and older iOS in the else's statements. However, Apple did announce a new system called Appearance in anywhere to support that new iOS 13 system.
let app = UINavigationBarAppearance()
let navigationBar = self.navigationController?.navigationBar
app.configureWithOpaqueBackground()
app.shadowImage = UIImage()
self.navigationController?.navigationBar.scrollEdgeAppearance = app
navigationBar!.standardAppearance = app
navigationBar!.scrollEdgeAppearance = app
I believe this configureWithOpaqueBackground() allows us to remove the navigation bar background, But I test on iOS 13.1 simulator appear black navigation bar background. I know what caused it.
app.configureWithOpaqueBackground()
app.titleTextAttributes = [.foregroundColor: UIColor.white]
app.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
app.backgroundColor = #colorLiteral(red: 0.1603881121, green: 0.1677560508, blue: 0.2133775949, alpha: 1)
That code allows us to put the custom color on the black background. I ready to add that code in the viewWillDisappear's statements to restore the navigation bar background to normal color background before self.navigationController?.navigationBar.scrollEdgeAppearance = app with remove app.configureWithOpaqueBackground() and app.shadowImage = UIImage(). Now, I need to create the translucent navigation bar background in the viewWillAppear's statements, but it can't see any translucent background due to the black background still display.
I really appreciate your help in resolving the problem! :)
If you want the navigation bar to become completely transparent:
let app = UINavigationBarAppearance()
app.configureWithTransparentBackground()
self.navigationController?.navigationBar.standardAppearance = app
self.navigationController?.navigationBar.scrollEdgeAppearance = app
self.navigationController?.navigationBar.compactAppearance = app
Do not mess with the isTranslucent of the navigation bar.
When I updated to Xcode 11 all my NavigationBar have become transparent (Transparent bar photo). Why, I didn't change any code after the update?
This is the new behavior of iOS 13 as it is mentioned here:
In iOS 13 and later, a large title navigation bar doesn’t include a background material or shadow by default.
You can still override this behavior:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .white
self.navigationController?.navigationBar.scrollEdgeAppearance = appearance
I have a very specific issue. When I set the background barTintColor to my blue, it's too light. Nothing I do seems to make it 100% accurate.
So I changed the code to set the nav bar background to 100% black. Using the OS X app SIP to analyze the color, or just setting the view to black as well, it's pretty obvious the color is very dark gray, but not black.
What is making the tint color screw up? As it stands, the blue I need and what the nav bar is showing are not the same.
navigationController?.navigationBar.setBackgroundImage(UIImage.imageFromColor(UIColor.black), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.barStyle = .blackOpaque
navigationController?.navigationBar.isOpaque = true
navigationController?.navigationBar.barTintColor = UIColor.black
Also in a blank project, fresh, same issue.
The key is to set isTranslucent to false.
let navigationBar = navigationController?.navigationBar
navigationBar?.barTintColor = .black
navigationBar?.isTranslucent = false