Changing navigation bar back button image size globally - ios

Currently I am using the following code to produce a custom back button image for my navbars:
appearance.backIndicatorImage = UIImage(named: "arrow-back")
appearance.backIndicatorTransitionMaskImage = UIImage(named: "arrow-back")
Unfortunately my image is large so it looks better on retina devices, and this method just displays the default size. Any way to shrink it down to get it fitting nicely?

My code will help you
let btnImg = UIImage(named: "Location_OnMap")
let btn = UIBarButtonItem(image: btnImg, style: .Plain, target: self, action: "doSomthing")
self.navigationItem.leftBarButtonItem = btn

Related

Safari-style back button

I've not found on stackoverflow a modern solution how to make a Safari-style back button for UIBarButtonItem. Is there a simple and elegant way to implement a back button as it is in Safari?
If you are targeting iOS13 and above, you can use the built-in SF Symbols library provided by Apple.
The chevron.left image is used for the Safari back button.
let chevronLeft = UIImage(systemName: "chevron.left")
let backButton = UIBarButtonItem(image: chevronLeft, style: .plain, target: webView, action: #selector(webView!.goBack))
These icons also come in nine weights from ultralight to black, which can be applied like this.
let chevronLeft = UIImage(systemName: "chevron.left", withConfiguration: UIImage.SymbolConfiguration(weight: .thin))
More information is available from Apple here: SF Symbols
I have found solution. The best way is to find back button image on web and use this code:
let backIcon = UIImage(named: "backIcon")
let backButton = UIBarButtonItem(image: backIcon, style: .plain, target: webView, action: #selector(webView!.goBack))
For example:

How to set a Custom image programmatically to and UIBarButtonItem

I am having a problem assigning a custom image to an UIBarButtonItem, the main problem is that the image appears as an white square when create the button.
here is my code:
fileprivate func configureNavigationBar() {
tabBarController?.navigationItem.title = lot.name
let exportImg: UIImage = UIImage(named: "action.png")!
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(showCreationView(_:)))
let exportByEmail = UIBarButtonItem(image: exportImg, style: .done, target: self, action: #selector(exportDataByEmail(_:)))
tabBarController?.navigationItem.rightBarButtonItems = [exportByEmail,addButton]
}
The problem is with the exportByEmail, image is in the variable exportImg added from my assets:
The result obtained from my code:
Your image's background must be transparent, and you can set always original rendering mode to image, to display without changes as following
let exportByEmail = UIBarButtonItem(image: exportImg.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(exportDataByEmail(_:)))

setBackButtonBackgroundImage not working

I am simply trying to change the back button icon in the UINavigationBar to a custom image so it matches all the rest of the icons used, however I have approached this two different ways.
One setting the back image an mask image in the UINavigationController within the storyboard inspector to the image, which resulted in the image not being in line with the title or rightBarItem.
Another method I have tried is setBackButtonBackgroundImage within the ViewController file or appDelegate. However the following code;
let backImg: UIImage = UIImage(named: "Back")!
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)
But the result of this was far more weird, the image becomes stretched.
Can anyone help me out on why this is happening or give an alternate method to change the back icon in the UINavigationBar ?
let backImage = UIImage(named: "back_button_name")
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImage?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, (backImage?.size.width)!-1, 0, 0)), forState: .Normal, barMetrics: .Default)
Swift 3.0
let backImage = UIImage(named: "arrow_left")
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage

UIBarButtonItem: Image offsets

I am trying to create custom back bar button item using image. I've done it this way:
let image = UIImage(named: "Back")
self.navigationBar.backIndicatorImage = image
self.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Back")
self.navigationBar.tintColor = UIColor.blackColor()
But here's result:
As you can see, it should be moved a little down and a little right. I tried to add offset to image this way:
self.navigationBar.backIndicatorImage = image?.imageWithAlignmentRectInsets(UIEdgeInsetsMake(10, 40, 0, 0))
But it does not work, the same. Any ideas?
Although, I feel, there is a scope to move title bit up to match with back button, you can move nav bar button down by:
UINavigationBar.appearance().setTitleVerticalPositionAdjustment(5.0, forBarMetrics: UIBarMetrics.Default)
To move it to right, try adding a flexible space, something like:
let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)

How to add / use default icons to navigation bar

I want to use some of default iOS icons i.e.
in navigation bar.
Basically I don't know how to call image of that item (directly from native library - I know how to download it and place as custom image:)):
var myButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
myButton.addTarget(self, action: "reload", forControlEvents: UIControlEvents.TouchUpInside)
myButton.setImage(???, forState: <#UIControlState#>)
You can use UIBarButtonSystemItem this way:
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "someAction")
navigationItem.leftBarButtonItem = button
Result for leftBarButtonItem:
If you want to set it at right side you can use this code:
navigationItem.rightBarButtonItem = button
Result for rightBarButtonItem:
Swift: These are the most commonly used options:
To Use custom image with original colour:
let customImageBarBtn1 = UIBarButtonItem(
UIImage(named: "someImage.png").withRenderingMode(.alwaysOriginal),
style: .plain, target: self, action: #selector(handleClick))
To Use custom image with tint colour:
let customImageBarBtn2 = UIBarButtonItem(
UIImage(named: "someImage.png").withRenderingMode(.alwaysTemplate),
style: .plain, target: self, action: #selector(handleClick))
Or use system provided buttons:
let systemBarBtn = UIBarButtonItem(
barButtonSystemItem: .search,
target: self, action: #selector(handleClick))
Then add any one of these buttons to the navigationItem:
navigationItem.leftBarButtonItems = [customImageBarBtn1, customImageBarBtn2]
navigationItem.rightBarButtonItems = [systemBarBtn]
// OR you can use this if there's only one item.
navigationItem.rightBarButtonItem = systemBarBtn
For custom Images: As a starting size, 22ptx22pt images work well for the default iPhone Navigation Bar size.
In swift 4.3
let btnRefresh = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.refresh, target: self, action: #selector(targeted function to invoke))
//If you want icon in left side
navigationItem.leftBarButtonItem = btnRefresh
//If you want icon in right side
navigationItem.rightBarButtonItem = btnRefresh

Resources