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()
Related
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()
}
Sample project with the issue can be found here
I have a requirement where I am going from a UIViewController inside a UINavigationController with large titles set to true to a UIViewController where the NavigationBar is transparent.
I have done this following numerous StackOverflow answers by setting the backgroundImage and shadowImage of the NavigationBar to be an empty UIImage instance:
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
The issue I am having is when I go back from this UIViewController to the first one the background image has gone and appears as a clear image if I set them to nil.
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
As can be seen in the images below.
How can I get the NavigationBar back to the original state?
The NavigationBar in iOS11 has a nice blur to it. I'm trying to replicate this to another image.
The following will set the NavigationBar with the default values (that include the blur):
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.setBackgroundImage(nil, for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = nil
I'm simply trying to copy the background to a new image to match the look but the following does not work:
let imageView = UIImageView()
imageView.image = self.navigationController.navigationBar.backgroundImage(for: UIBarMetrics.default)
Any ideas?
So I never could figure this out but I was able to solve it by using a blank UIToolbar where I needed to replicate the navBar background.
In my case I was trying to set the statusBar to have the same background and the navigationBar.
statusBarView = UIToolbar()
To set it with the default background:
self.statusBarView.setBackgroundImage(nil, forToolbarPosition: .any, barMetrics: .default)
And to make it transparent:
self.statusBarView.setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
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
I am trying to use an image as the background for my navigation bar. I can get the image into the bar, but it doesn't appear properly. I have attached an image of the desired background, and the result that I am getting.
In addition this is the code I am using to get the image into the bar background.
navigationController.navigationBar.setBackgroundImage(UIImage(named:"Top Bar Slice"), forBarMetrics: UIBarMetrics.Default)
EDIT:
One of the answers got the image into the code, but now it is tile-ing across the bar. Is there a way to fix this?
I am now using this code in the app delegate:
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "Top Bar"), forBarMetrics: UIBarMetrics.Default)
You need to override UINavigationBar.appearance()
Option 1:
let image = UIImage(named: "yourImage")
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: UIBarMetrics.Default)
Option 2:
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "yourimage"), forBarMetrics: UIBarMetrics.Default)
You can also add a titleView to the navigation item in your viewController.
Option 3:
navigationItem.titleView = UIImageView(image: UIImage(named: "yourimage"))