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:
Related
This question already has answers here:
How to add Badges on UIBarbutton item?
(10 answers)
Closed 4 years ago.
I want to add a badge icon on an icon which i have used in UINavigationBar.
This is so far i have done
let threeDotButton = UIBarButtonItem(image:UIImage(named: "ic_more.png")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(DashboardViewController.threeDotImageClick))
let notificationButton = UIBarButtonItem(image:UIImage(named: "ic_notifications.png")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(DashboardViewController.notificationImageClick))
self.tabBarController?.navigationItem.rightBarButtonItems = [threeDotButton,notificationButton]
let title = UILabel()
title.text = "Dashboard"
self.tabBarController?.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: title)
if let fontstyle = UIFont(name: "Roboto", size: 30) {
self.tabBarController?.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: fontstyle]
}
I want to add count badge on notifcation button . I searched about it but i am not getting a proper solution about it . can anyone explain me how to do it and achieve it
https://github.com/TanguyAladenise/BBBadgeBarButtonItem try this pod it may satisfy your requirement but its written in objective C but fairly easy to use !!!
In our app we are showing a burger item in the UINavigationBar.
We are using the leftBarButtonItem as the place to show it.
Here is the code to create the burger button.
let barItem = UIBarButtonItem(image: UIImage(named: "IconBurger"), style: .plain, target: target, action: selector)
barItem.tintColor = .tintColor
barItem.adjustAccessibility()
Which leads to following result on iOS 11.4 and iOS 11.3.1
iOS11.4
iOS10.3.1
As you can see the burger button somehow shrinked on iOS11.4
I fixed this by creating a custom button view like this:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "IconBurger"), for: .normal)
button.addTarget(target, action: selector, for: .touchUpInside)
let barItem = UIBarButtonItem(customView: button)
barItem.tintColor = .tintColor
barItem.adjustAccessibility()
Using this version the burger button is looking good on iOS 11.4 again.
But now when going back to 10.3.1 I was shocked because the burger button was not rendered at all anymore.
(Imagine completely black image here)
I ended up writing ugly stuff like
if #available(iOS 11.4, *) {
// show new version
} else {
// show old version
}
But I hope that can't be it!
Does anybody experienced similar or can give advise ?
Additional information: We are using pdf assets for creating UIImages in our project.
Ok. Colleague of mine found the solution which I want to document here.
Turns out calling sizeToFit() was missing on < iOS11
let button = UIButton(type: .custom)
let image = UIImage(named: "IconBurger")
button.setImage(image, for: .normal)
button.addTarget(target, action: selector, for: .touchUpInside)
button.sizeToFit()
let item = UIBarButtonItem(customView: button)
item.adjustAccessibility()
return item
Hopefully, this is an easy question. I am trying to obtain the UIView of a UIBarButtonItem. Looking at the following StackOverflow question I came up with the following code:
let notificationButton = UIBarButtonItem(image: UIImage(named: "icon_notification.png"), style: .plain, target: self, action: nil)
let notificationView = notificationButton.value(forKey: "view") as? UIView
However, notificationView is nil. So, thoughts on what I failed to interpret from the linked StackOverflow question?
Starting from iOS 11 this code will not work cause line of code below will not cast UIView. Also it's counting as private API and seems to be will not pass AppStore review.
guard let view = self.value(forKey: "view") as? UIView else { return }
Thread on: forums.developer.apple
So, DS Dharma gave me a idea which ended up working. The "view" value is only available after it is assigned to the toolbar navigation item like this:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_notification.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem?.addBadge(number: 0, withOffset: CGPoint(x: 7.0, y: 0.0) , andColor: UIColor.black, andFilled: true)
where the addBadge() function needs the UIView. BTW, if anyone is wondering, the addBadge function was taken from this post: http://www.stefanovettor.com/2016/04/30/adding-badge-uibarbuttonitem
Highly recommended if you need this piece of functionality.
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
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