How is it possible to customize the navigation bar like in the AppStore app?
I already tried to set the following (it is a subclass of UINavigationController):
self.navigationBar.setBackgroundImage(UIImage.with(color: UIColor.red), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
self.view.backgroundColor = .blue
Then the large navigationBar is blue and the small/collapsed navigationBar is red. But what I have to adjust that I get a translucent navigationBar when the navigationBar is collapsed?
[
Related
I have tested this on a sample project with 2 view controllers defined in the storyboard using Xcode 11 (iOS 13). The "presenting" view controller is embedded in a navigation controller and has the navigation bar colors set in the viewWillAppear.
The "search" view controller adds a UISearchController in the viewDidLoad and is pushed by the presenting view controller (NOT modal).
With just this setup when the search view controller is displayed the navigation bar has the blue background and red tint color as expected. However when scrolling down and the search bar is displayed the background color of the navigation bar is lost (or changed to what appears to be the default iOS grey / translucent). However if you scroll back up (hide the search bar) or focus on the search bar text field the navigation bar color returns!
Also if you focus on the search bar text field and then cancel (by tapping the Cancel button) the tint color of the navigation bar reverts from red to the default iOS blue as can be noticed by the back bar item.
Any suggestions on resolving this issue?
I have set the navigation bar colors in the viewWillAppear of the search controller too which didn't change this behaviour.
I set isTranslucent to true for the navigation bar in the search controller which seemed to prevent the reverting of the background color but it did not change the reverting of the tint color on cancel.
Presenting View Controller
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.tintColor = .red
}
Search View Controller
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Search VC"
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true
}
EDIT
Setting the scrollEdgeAppearance, backButtonAppearance and buttonAppearance as suggested works a treat except for system bar buttons that default to the iOS blue. This can be resolved by setting the UINavigationBar.tintColor but neither that resolves the back button chevron color defaulting on cancel of the search.
if #available(iOS 13.0, *) {
let buttonAppearance = UIBarButtonItemAppearance()
buttonAppearance.configureWithDefault(for: .plain)
buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.red]
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .blue
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.red]
navigationBarAppearance.backButtonAppearance = buttonAppearance
navigationBarAppearance.buttonAppearance = buttonAppearance
navigationBarAppearance.doneButtonAppearance = buttonAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navigationBarAppearance
navigationController?.navigationBar.compactAppearance = navigationBarAppearance
navigationController?.navigationBar.standardAppearance = navigationBarAppearance
} else {
navigationController?.navigationBar.barTintColor = .blue
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.red]
navigationController?.navigationBar.tintColor = .red
}
However when scrolling down and the search bar is displayed the background color of the navigation bar is lost
All of that is normal. New in iOS 13, the expanded nav bar (displaying search bar, large title, etc.) has different appearance from the normal nav bar. Your settings applied only to the normal nav bar because you didn't make them the iOS 13 way. If you want the expanded nav bar to look like the normal nav bar, you have to set its appearance separately and explicitly.
To do so, you need to set the navigation bar's scrollEdgeAppearance. Investigate classes UIBarAppearance, UINavigationBarAppearance, and UIBarButtonItemAppearance (you will need to set the backButtonAppearance explicitly).
Is there a way we can customize the code below on a nav bar which is from the object library and not a nav bar which is embedded in? I have the code below. It's working on a nav bar that is embedded on my view but when I create my own nav bar from the object library, the code below is no longer working.
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Chalet-NewYorkNineteenEighty", size: 37.0)!];
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "BG.jpg")!.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
For the most part I assume you are creating UINavigationBar in xib, in that case IBOutlet wont be UINavigationBarController, give this a try
navigationBar.tintColor = UIColor.white
navigationBar will be outlet var name...
If this doesn't work more info might help...
I have a navigation bar created by the nav controller for my view, these are loaded via a container side menu.
When i click an item, it loads the nav controller and view, but the nav bar background drops out showing a blank background colour on the status bar.
Any idea how I can diagnose this issue? I have included some view debugger screenshots to best illustrate
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor().appThemeColour()
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIApplication.shared.statusBarStyle = .lightContent
Well you are missing one thing here and thats setBackgroundImage of UINavigationBar.
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor().appThemeColour()
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
UIApplication.shared.statusBarStyle = .lightContent
*Note this is the solution for Sidemenu Library named "https://github.com/jonkykong/SideMenu/"
Change status bar end alpha property to 0
Please find code below.
#objcMembers
open class SideMenuPresentationStyle: InitializableClass {
/// Background color behind the views and status bar color
open var backgroundColor: UIColor = .white`
I have the nav bar transparent with only the bar button being displayed in my app. In my storyboard I have no title so that the nav bar looks like nothing is there, but when I run the simulator, the text that I deleted is in the nav bar.
This is what i've found to solve problem:
What the nav bar should look like without the transparency
What the nav bar looks like in the simulator
My code for making the bar transparent:
//Makes navigation bar translucent
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
In your code, inside view will appear method
use this code
self.title = "" // or whatever text you have
In your app delegate, did finish launching with option method:
UINavigationBar.appearance().barTintColor = .white
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.black] // shouldn't be needed, but if you want something
UINavigationBar.appearance().tintColor = .blue
From your storybord remove any changes done by you. It can be done from code, also since it needs to be consistent in the app. Try to handle status bar and navigation bar from code itself.
I recently migrated from Swift 2 to Swift 3 and I'm stuck on one part. In my AppDelegate, I'd set up the default settings for the UINavigationBar as coded below. The challenge I'm having is that the setBackgroundImage is no longer recognized in Swift 3 and I can't find an alternative.
Has anyone had the same issue happen and been able to resolve it?
let turquoiseBackgroundImage:UIImage = UIImage(named: "navigationBarTurqoise")!
**// TODO Below is not working with Swift 3
UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, forBarPosition: .Default)**
// Set up Back button to be white in Navigation bar
UINavigationBar.appearance().barStyle = UIBarStyle.default
UINavigationBar.appearance().tintColor = UIColor.white
// Font Name and Size of Title in Navigation Bar
if let font = UIFont(name: "TitilliumWeb-Light", size: 20) {
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : font, NSForegroundColorAttributeName : UIColor.white]
}
// Remove hairline between navigation bar and anything below such as search bar
UINavigationBar.appearance().shadowImage = UIImage()
// Set up status bar (battery, time, etc.) to white
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, forBarPosition: .Default)
rewrite code like this
UINavigationBar.appearance().setBackgroundImage(turquoiseBackgroundImage, for: .default)