I have a two TableViewControllers with Embedded Navigation Controllers. ViewController-1 shows the hairline/shadow under the nav bar and ViewController-2 doesn't show it when I navigate to it. Navigation is done using the Push Segue
To hide the navigation bar in ViewController-2 I adding the following lines in ViewWillAppear the following:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = false
When I use the back button to go back to ViewController-1 the Hairline disappears there too but I do not want it to. Is there anyway to ensure that the hairline doesn't hide in ViewController-1?
I have tried :
self.navigationController?.navigationBar.barStyle = UIBarStyle.Black
and
self.navigationController?.navigationBar.backgroundColor = UIColor.whiteColor()
but without much success.
In the viewWillDisappear (or maybe the viewDidDisappear) method of ViewController-2, add code to undo the changes made in viewWillAppear.
self.navigationController?.navigationBar.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.translucent = true
Related
Is it still possible to customize the navigation bar on ScrollViewDidScroll now?
For example:
Change it using:
self.navigationController?.navigationBar.isTranslucent = False
And remove:
self.navigationController?.navigationBar.setBackgroundImage...
self.navigationController?.navigationBar.shadowImage = UIImage()
Then modify the barTintColor when user scrolls.
I have disabled the bottom border line (shadow image) of the navigation bar using:
self.navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController.navigationBar.shadowImage = UIImage()
But after locking the device and unlocking it again the bottom border line appears. I was able to hide it again by using the above code in viewWillAppear:
But I would like to know what's causing the navigation bar to reset to it default configuration each time it appears?
As I am unable to make the shadow line reappear in another ViewController using below code:
self.navigationController.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController.navigationBar.shadowImage = nil
I would like to know how to reset the navigation bar appearance to make the bottom border line(shadow image) reappear?
Unlocking device will NOT call viewWillAppear, May be you are using a BaseViewController with adding Notification Observer on UIApplicationDidBecomeActiveNotification
Try using Appearance instead on AppDelegate
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
UINavigationBar.appearance().shadowImage = UIImage()
And Reappear the line again
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
You may try to create UINavigationViewController extension with a handy function to do it:
extension UINavigationController {
func resetToDefaultAppearance() {
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
navigationBar.standardAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
navigationBar.standardAppearance = appearance
navigationBar.compactScrollEdgeAppearance = appearance
}
}
Then call it in your viewWillDisappear:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.resetToDefaultAppearance()
}
I want to clear color of navigation bar. In my ViewController there is a background image on that, when i remove color of navigation barTintColor, navigationController.view.background and navigation background image then simulator shows me :-
I have been trying alots of codes but there is no solution found.
I want navigation Bar like that:-
with clear navigation bar color.
Is there any solution, let me know?
Thanks!
You can make the navigation bar transparent in viewWillAppear and remove transparency in viewWillDisappear as follows
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.isTranslucent = false
}
The background image and the back button will be visible
Better you must avoid the navigation bar. Hide the navigation bar in the navigation controller and user custom view in your view controller to avoid this issue.
Swift 5:: Calling below in AppDelegate's didFinishLaunchingWithOptions function does the trick (This will be applied to your all navigationBars though, don't forget to switch your view controllers)
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
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 create an app with Swift 3 and Xcode 8.1, I have a view controller with navigation bar, my view shows a line (separator) between the bar and other viewController's content.
I use following code in viewDidLoad:
self.navigationController?.navigationBar.isTranslucent = false
But nothing changed, for more details here's a screenshot:
What I can do to solve that?
Try:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
And if you want to apply this effect to the whole app (so that you don't need to write this code for every navigation controller) you can use:
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()