How to copy the default NavigationBar background image in iOS11? - ios

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)

Related

AppStore NavigationBar iOS 13

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?
[

How to set custom navigation controller back arrow image throughout app in the app delegate file?

I want to change all the back arrows in all navigation controllers throughout my app. I want to avoid changing the image manually in every file as that would be time-consuming. I'll post the code I have so far in my AppDelegate file in the didFinishLaunchingWithOptions method.
let backButton = UIImage(named: "backbutton-thin")?.resizableImage(withCapInsets: UIEdgeInsetsMake(0, 0, 16, 16))
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 5, topCapHeight: 0)
barButtonAppearance.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
let item = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationController.self])
item.setBackButtonBackgroundVerticalPositionAdjustment(0, for: .default)
item.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
The code produces this result in all my navigation bars ...
As you can see, the thin arrow is overlapping the "Back" text and the original arrow is still there. What am I doing wrong? How can I remove the original arrow and replace it with my thin custom arrow? I still want to keep the text though. Any help would be appreciated.
You need to modify the backIndicatorImage and backIndicatorTransitionMaskImage of the UINavigationBar class to replace the existing back indicator image with your custom one.
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "backbutton-thin")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "backbutton-thin")

iOS: Line between navigation top bar and other contents of UIviewController

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()

Swift 3: How to set up UINavigationBar setBackgroundImage in AppDelegate

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)

Using custom image as background of Navigation bar of UINavigationController

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"))

Resources