Swift Navigation Bar not change userInterfaceStyle on scoll - swift

I have a problem when I try to change interface style (dark or light). If I set dark mode I get the navigation bar remains light when I scroll. If I set light mode the navigation bar becomes black. How can I solve this?
I use this code to change userInterfaceStyle:
window?.overrideUserInterfaceStyle = .dark

I found a solution by myself.
For everyone that has this problem the solution is:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .systemBackground
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = UINavigationBar.appearance().standardAppearance
put this code inside TabBarController

Related

How to remove strange highlighting of UINavBar when scrolling UICollectionView?

I have a child navigation view controller with a collectionView embedded in it. When I scroll it, the navigation bar appears to be dimmed a bit.
It looks like this:
Does anyone know how to remove this behavior?
I was setting my navigation bar appearance globally like that:
let appearance = UINavigationBarAppearance()
appearance.shadowImage = UIImage()
...
UINavigationBar.appearance().standardAppearance = appearance
But I finally managed to find the fix. You just have to add this:
appearance.backgroundEffect = nil
and the problem goes away.

Xcode 13 - Navigation bar and status bar text color changing in swift OS 15

Recently I updated my Xcode to 13 and after that, I am facing some issues with the navigation bar and Status bar. I am using the tab bar in my view controller. After updating the Xcode, according to the version, I added some code related to the navigation bar.
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
Everything is working when I first opened the app. When I click on the other tab and then this tab. The status bar text color is changing.
I tried different ways to set the status bar text color. But nothing worked for me.
Use this function:
extension YourViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
}
If you just want to set to white colour you status bar in the entire project you can easily do it by adding the following keys into your project.plist file:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
So, what does these keys do... You are just defining that you status bar colour will not be controlled by the any view controller and that the default colour will be "light theme" that is pretty much the white colour that you want in this case...
this would be the best approach if you do not intent to update the status bar colour all over the place.
this was tested and is working as expected on Xcode 13 and iOS [13.x,14.x,15.x]
If you want to update to customise or update the colours for the navigation bar you would should be able to do with an extension like this one bellow, just few examples but you guys should get what I mean:
extension UINavigationBar {
func clearBackground() {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navBarAppearance.shadowColor = UIColor.clear
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
func blackColor() {
// Create an Appearance and customise as you wish.
// in this case just a black background with titles in white.
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = UIColor.black
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
}
with this code you should be able to easy update on any view controller by just calling something like:
override func viewDidLoad() {
super.viewDidLoad()
// set the navigation bar color as transparent
self.navigationController?.navigationBar.clearBackground()
}

Navigation bar and tab bar turned black on iOS15 Xcode 13

Today I just updated my Xcode to version 13 and found that all the navigation bars and the tab bars of my project turned black, I didn't change any settings, my project was working fine on Xcode 12, and I have toggled it to light mode, I couldn't find a way to recover the appearances to the old ones.
Just simply check the "Translucent" in your attributes inspector.
Apply the following code to update the appearance of the navigation bar. This can be done in AppDelegate if you wish for it to be the appearance throughout the app.
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: .black]
appearance.backgroundColor = .white
// Default portrait view
UINavigationBar.appearance().standardAppearance = appearance
// There are other appearances you could apply it to as well...
// UINavigationBar.appearance().scrollEdgeAppearance = appearance
// UINavigationBar.appearance().compactAppearance = appearance
Make like this:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .red
appearance.titleTextAttributes = [.font:
UIFont.boldSystemFont(ofSize: 20.0),
.foregroundColor: UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
I wrote a new article talking about it.
https://medium.com/#eduardosanti/uinavigationbar-is-black-on-ios-15-44e7852ea6f7

How do I change the default status bar color when overriding system UI style in swift

When I override overrideUserInterfaceStyle to light and dark mode is set to on, on the device, it is not changing the status bar style to .lightcontent.
overrideUserInterfaceStyle = .light
self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.black]
self.navigationController?.navigationBar.barStyle = .default
self.setNeedsStatusBarAppearanceUpdate()
That is my code for overridng the interfacce style. When I do this the statusbar is staying with dark content.
If your content is inside a UINavigationController, you won't get what you expect. You will have to subclass UINavigationController to reach into its content and promote that view controller's overridden value.
See this thread for some answers: preferredStatusBarStyle isn't called

how to remove navigationbar blur effect - swift

i searched a lot on internet but did not find any solution for the problem, i don't want the navigation bar to be blur as in the screenshot
the colour of navigation bar does not match with the other colour even i have used the sam hex value for both. i want to fix it. please help
this is the code i am using to colour navigation bar
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.white
navigationBarAppearace.barTintColor = UIColor(red: 204/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0)
What you see isn't actually blur effect, but translucent style of UINavigationBar. If you don't want translucent bar, set its isTranslucent property to false
UINavigationBar.appearance().isTranslucent = false
Use:
navigationController.navigationBar.isTranslucent = false
You can disable this blur effect by using both standard and scroll edge
By Setting
**Standard Appearance
background-color**
This setting will Work to hide the blur effect on a scroll and also to set title style on scroll
**Scroll Edge Appearance
background-color**
Appearance without scroll-like background color, title, etc.
Other Settings are remained the same like
Translucent: true etc