navigation bar style change while iphone theme is in dark mode - ios

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
}

Related

Customize MFMailComposeViewController

I have an issue with customizing the appearance of the MFMailComposeViewController on iOS 13.
My application uses a dark navigation bar and white tint on items in the bar.
UINavigationBar.appearance().tintColor = BrandManager.globals.textColor
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UINavigationBar.appearance().tintColor]
// navBar color for app
UINavigationBar.appearance().barTintColor = BrandManager.primaryColors.background
// navBar color for some sharing containers…except MFMessageComposeVC & SLComposeVC (twitter)
UINavigationBar.appearance().backgroundColor = BrandManager.primaryColors.background
// navBar color for MFMessageComposeVC & SLComposeVC
UINavigationBar.appearance().setBackgroundImage(UIImage(color: BrandManager.primaryColors.background), for: .default)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = BrandManager.globals.textColor
This worked in iOS 12 and prior.
For iOS 13, I'm getting the following, very strange behavior, where the bar button items are white, the title text is black, and scrolling up produces this strange translucent white on the correct navigation bar color.
I have tried every combination I can think of for the UIBarButtonItem appearance but nothing seems to work. Most other solutions I've found aren't related to iOS 13.

How I can remove navigation bar background with appearance in the iOS 13?

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.

Visible UISearchBar changes UINavigationBar background color

A tableview controller is embedded into a navigation controller.
I programmatically added a search bar to the tableview controller's navigation bar. I only changed the navigation bar Background color into something different than Default (purple) - all the rest I left default.
class TableViewController: UITableViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesSearchBarWhenScrolling = true
navigationItem.searchController = searchController
}
}
Code above is reduced to bare minimum for demonstration purpose.
All done with Xcode 11 (11A420a).
I ran the project in iOS 12.0 and 13.0 simulators and devices.
iOS 13.0
The search bar shows upon start.
Navigation bar background color is correctly presented.
While scrolling, navigation bar background color remains correct.
With iOS 13.0, all works as expected!
iOS 12.0
The search bar doesn't show upon start.
Navigation bar background color is correctly presented.
While scrolling, navigation bar background color goes white as soon search bar is visible.
I tried to change all kind of color setting in storyboard as well as properties programmatically. I didn't succeed in changing the navigation bar background color when search bar is visible.
It seems (?!) that the navigation bar foreground looses transparency when search bar becomes visible.
If I use a Bar Tint color of the navigation bar (!= Default), all works as expected (and as with iOS 13.0), but I loose the gradient effect, which I would like to keep.
What did I miss?
How can I avoid this?
Bug?
I had some luck with the navigationItem.scrollEdgeAppearance property when facing a similar problem. For example:
vc.navigationItem.scrollEdgeAppearance?.backgroundColor = .red
This is only available on iOS 13 though.
Here's what I ended up doing to get correct colors in the navigation bar while allowing the search controller's scroll bar to hide during scroll:
if #available(iOS 13.0, *) {
let blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)
navbar.standardAppearance.backgroundEffect = blurEffect
navbar.standardAppearance.backgroundColor = appMainColor.withAlphaComponent(0.75)
navbar.standardAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navbar.compactAppearance = nil
navbar.scrollEdgeAppearance = navbar.standardAppearance.copy()
navitem.standardAppearance = nil
navitem.compactAppearance = nil
navitem.scrollEdgeAppearance = nil
}
I don't know if it is the look you're going for but I found if you disable hideSearchBarWhenScrolling the background stops changing color. However, the search bar is always there.
Add this to viewDidLoad():
navigationItem.hidesSearchBarWhenScrolling = false

UINavigationBar appearance + iOS 13 Dark Mode - How to set app wide color?

In iOS 12 and before I used [[UINavigationBar appearance] setBackgroundColor...] to set a app wide background color for all NavBars.
In iOS 13 I would like to do the same while supporting Dark Mode. In the apps Asset Catalog I defined a named Color NavBarBackground and specified both a Any appearance and Dark appearance color.
If the Dark Mode is disabled the correct Any color is used. However when Dark Mode is enabled the specified Dark color is ignored and all NavBar appear in plain black...
However, if I set the background color of a NavBar manually in IB to NavBarBackground this one NavBar shows the correct color both in Normal and in Dark Mode.
So, how to use [UINavigationBar appearance] together with Dark Mode and named colors?
You can use iOS 13's new appearance API:
https://developer.apple.com/documentation/uikit/uinavigationbarappearance
Example:
let style = UINavigationBarAppearance()
style.backgroundColor = .red
style.barTintColor = UIColor(named: "my_colour")!
navigationController?.navigationBar.standardAppearance = style
navigationController?.navigationBar.scrollEdgeAppearance = ...
navigationController?.navigationBar.compactAppearance = ...

Navigation Bar is transparent swift 5

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

Resources