Navigation Bar is transparent swift 5 - ios

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

Related

Why the view.safeAreaInset is same for Opaque and Transparent UINavigationBarAppearance?

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.

navigation bar style change while iphone theme is in dark mode

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
}

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

Resources