Set image and title for bar button item? - ios

I currently have a custom navigation controller with bar button items that are simply text buttons. Is it possible to keep the title of the bar button items but also set them as images (icon image + title underneath).
class NavigationController: UINavigationController
{
var mode: NavigationMode = .Swipe {
didSet {
self.setButtonAttributes()
}
}
private var leftBarButton: UIBarButtonItem!
private var middleBarButton: UIBarButtonItem!
private var rightBarButton: UIBarButtonItem!
private var rightBarButton2: UIBarButtonItem!
override func viewDidLoad()
{
super.viewDidLoad()
}
func configureNavigationItem(navigationItem: UINavigationItem)
{
//Configure the bar buttons text and actions
if (self.leftBarButton == nil) {
self.leftBarButton = UIBarButtonItem(title: "Menu1", style: .Plain,target: self, action: "menu1Pressed:")
}
if (self.middleBarButton == nil) {
self.middleBarButton = UIBarButtonItem(title: "Games", style: .Plain, target: self, action: "gamesPressed:")
}
if (self.rightBarButton == nil) {
self.rightBarButton = UIBarButtonItem(title: "Menu3", style: .Plain, target: self, action: "menu3Pressed:")
}
if (self.rightBarButton2 == nil) {
self.rightBarButton2 = UIBarButtonItem(title: "Settings", style: .Plain, target: self, action: "settingsPressed:")
}
self.setButtonAttributes()
navigationItem.leftBarButtonItems = [self.leftBarButton, self.middleBarButton, self.rightBarButton, self.rightBarButton2]
}
Updated:
let button = UIButton(type: .System)
button.setImage(UIImage(named: "play"), forState: .Normal)
button.setTitle("Play", forState: .Normal)
button.sizeToFit()
leftBarButton = UIBarButtonItem(customView: button)
if (self.leftBarButton == nil) {
self.leftBarButton = UIBarButtonItem(title: "Play", style: .Plain,target: self, action: "Pressed:")
}

You can create UIButton instance, set an image and a title for it, and then create your UIBarButtonItem with it:
let button = UIButton(type: .System)
button.setImage(UIImage(named: "YourImage"), forState: .Normal)
button.setTitle("YourTitle", forState: .Normal)
button.sizeToFit()
self.leftBarButton = UIBarButtonItem(customView: button)
To add an action:
button.addTarget(self, action: #selector(self.someAction), forControlEvents: .TouchUpInside)
where self.someAction is
func someAction() {
}

Create an UIButton, set an image and a title for it and use it as a custom image to init your UIBarButtonItem(customView:) with it.
If you want the image to be on the right side of the button, you can set the button's semanticContentAttribute to .forceRightToLeft.
Swift 4 example:
let view = UIView()
let button = UIButton(type: .system)
button.semanticContentAttribute = .forceRightToLeft
button.setImage(UIImage(named: "DocumentsIcon"), for: .normal)
button.setTitle("Documents", for: .normal)
button.addTarget(self, action: #selector(openDocuments), for: .touchUpInside)
button.sizeToFit()
view.addSubview(button)
view.frame = button.bounds
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)

Swift 3:
let button = UIButton(type: .system)
button.setImage(UIImage(named: "categories_icon"), for: .normal)
button.setTitle("Categories", for: .normal)
button.addTarget(self, action: #selector(showCategories), for: .touchUpInside)
button.sizeToFit()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

Related

can AnyOne explain how to properly add constraints to navigation bar buttons or what is wrong with this piece of code?

navigationBar.items = [navItem]
let searchImage = UIImage(named: "searchIcon")?.withRenderingMode(.alwaysOriginal)
let searchButton = UIBarButtonItem(image: searchImage, style: .plain, target: self, action: #selector(handleSearch))
let menubutton = UIBarButtonItem(image: UIImage(named: "menu")?.withRenderingMode(.alwaysTemplate), style: .plain, target: self, action: #selector(slideMenu))
let refreshButton = UIBarButtonItem(image: UIImage(named: "refreshIcon")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(handleRefresh))
//searchButton.tintColor = UIColor.yellow
// searchButton.t
navigationBar.delegate = self
navItem.setRightBarButtonItems([searchButton,refreshButton], animated: false)
navItem.setLeftBarButton(menubutton, animated: false)
The easiest way to add navigation buttons to navigation bar is use storyboard. If you decide add button programatically you can reach from ViewController.
self.navigationItem.leftBarButtonItems
self.navigationItem.rightBarButtonItems
If you have Navigation controller in your Main.storyboard, which is point to your ViewController, then that controller reach top navigation item. Apple leftBarButtonItem documentation say:
A custom bar button item displayed on the left (or leading) edge of the navigation bar when the receiver is the top navigation item.
So please first check you use top navigation item.
I offer you work with UIButtons:
let leftMenuButton = UIButton(type: .system)
leftMenuButton.setImage(UIImage(named:"your.png"), for: .normal)
leftMenuButton.addTarget(self, action: #selector(self.yourFunc(_:)), for: .touchDown)
let searchButton = UIButton(type: .system)
searchButton.setImage(UIImage(named:"yourSearch.png"), for: .normal)
searchButton.addTarget(self, action: #selector(self.yourSearchFunc(_:)), for: .touchDown)
let refreshButton = UIButton(type: .system)
refreshButton.setImage(UIImage(named:"yourRefresh.png"), for: .normal)
refreshButton.addTarget(self, action: #selector(self.yourRefreshFunc(_:)), for: .touchDown)
These buttons added to UIBarButtonItem:
let menuBarItem = UIBarButtonItem(customView: leftMenuButton)
let searchBarItem = UIBarButtonItem(customView: searchButton)
let refreshBarItem = UIBarButtonItem(customView: refreshButton)
In my case added this code under viewDidLoad:
self.navigationItem.rightBarButtonItems = [searchBarItem, refreshBarItem]
self.navigationItem.leftBarButtonItem = menuBarItem
For fixing space or add constraint here is great article

Change UIBarButtonItem Icon after pressed iOS swift

In the viewDidload method, I've declared a button and set RightBarButton...
let btnFavourite = UIButton(frame: CGRectMake(0,0,30,30))
btnFavourite.addTarget(self, action: "btnFavourite:", forControlEvents: .TouchUpInside)
btnFavourite.setImage(UIImage(named: "star"), forState: .Normal)
btnFavourite.setImage(UIImage(named: "star_filled"), forState: .Highlighted)
let rightButton = UIBarButtonItem(customView: btnFavourite)
self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
How do I pressed the button from image 'star.png' then change to 'star_filled.png'? and press the button from 'star_filled.png' to 'star.png'?
How to make two functions like
func btnFavourite()
{
//clicked the favourite button then image change to star_filled.png
}
fun btnUnfavourite()
{
//clicked the button then the bar button image change to star.png
}
Create Method which will update you Button based on the state of self.isFavourited
var isFavourited = false;//declare this above the viewDidload()
func updateRighBarButton(isFavourite : Bool){
let btnFavourite = UIButton(frame: CGRectMake(0,0,30,30))
btnFavourite.addTarget(self, action: "btnFavouriteDidTap", forControlEvents: .TouchUpInside)
if isFavourite {
btnFavourite.setImage(UIImage(named: "star_filled"), forState: .Normal)
}else{
btnFavourite.setImage(UIImage(named: "star"), forState: .Normal)
}
let rightButton = UIBarButtonItem(customView: btnFavourite)
self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
}
func btnFavouriteDidTap()
{
//do your stuff
self.isFavourited = !self.isFavourited;
if self.isFavourited {
self.favourite();
}else{
self.unfavourite();
}
self.updateRighBarButton(self.isFavourited);
}
func favourite()
{
//do your favourite stuff/logic
}
func unfavourite(){
//do your unfavourite logic
}
In the viewDidload method, call first time, i.e.
self.updateRighBarButton(self.isFavourited);//first time self.isFavourited will be false
I'm sure there's a more elegant way to do it, but this is what worked for me:
In viewDidLoad():
let share_button = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let fave_button = UIBarButtonItem(image: UIImage(named: "icon-star"), style: .plain, target: self, action: #selector(favorite))
let faved_button = UIBarButtonItem(image: UIImage(named: "icon-star-filled"), style: .plain, target: self, action: #selector(unfavorite))
if YOUROBJECT.faved{
navigationItem.rightBarButtonItems = [share_button, faved_button]
}
else{
navigationItem.rightBarButtonItems = [share_button, fave_button]
}
Then create the two #selector functions:
#objc func favorite(){
let share_button = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let faved_button = UIBarButtonItem(image: UIImage(named: "icon-star-filled"), style: .plain, target: self, action: #selector(unfavorite))
navigationItem.rightBarButtonItems = [share_button, faved_button]
YOUROBJECT.faved = true
}
#objc func unfavorite(){
let share_button = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
let fave_button = UIBarButtonItem(image: UIImage(named: "icon-star"), style: .plain, target: self, action: #selector(favorite))
navigationItem.rightBarButtonItems = [share_button, fave_button]
YOUROBJECT.faved = false
}

How to programmatically create a "Back" UIBarButton item in Swift?

I was able to create a UIBarButton item that can go back programmatically using the following code:
func backAction() -> Void {
self.navigationController?.popViewControllerAnimated(true)
}
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
self.navigationItem.leftBarButtonItem = backButton
}
The problem is that the back button doesn't have the left pointing arrow:
Is there a way to make it look like a regular back button with the arrow like this:
I would also like to know if there is a way to make the button title names as the title of the previous view controller, if that's possible.
Thanks
Below is the code by using UIButton with image you can add it as a customView for UIBarButtonItem
override func viewDidLoad() {
super.viewDidLoad()
var backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal) // Image can be downloaded from here below link
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}
func backAction() -> Void {
self.navigationController?.popViewControllerAnimated(true)
}
Download Link
For setting the title of backbutton with the previous view controller title you have to pass the Title as a String while presenting the controller make change to above code as
var titleStrFromPreviousController: String // This value has to be set from previous controller while presenting modal controller
backbutton.setTitle(titleStrFromPreviousController, forState: .Normal)
This may help.
Swift 3
override func viewDidLoad() {
super.viewDidLoad()
addBackButton()
}
func addBackButton() {
let backButton = UIButton(type: .custom)
backButton.setImage(UIImage(named: "BackButton.png"), for: .normal) // Image can be downloaded from here below link
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal) // You can change the TitleColor
backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
#IBAction func backAction(_ sender: UIButton) {
let _ = self.navigationController?.popViewController(animated: true)
}
Updated for Swift 4.2 - thanks to sam bing and silentbeep
Made some modifications on some colors and action's selector.
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .blue
self.navigationItem.title = title
self.navigationController?.navigationBar.barTintColor = .white
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: makeBackButton())
}
func makeBackButton() -> UIButton {
let backButtonImage = UIImage(named: "backbutton")?.withRenderingMode(.alwaysTemplate)
let backButton = UIButton(type: .custom)
backButton.setImage(backButtonImage, for: .normal)
backButton.tintColor = .blue
backButton.setTitle(" Back", for: .normal)
backButton.setTitleColor(.blue, for: .normal)
backButton.addTarget(self, action: #selector(self.backButtonPressed), for: .touchUpInside)
return backButton
}
#objc func backButtonPressed() {
dismiss(animated: true, completion: nil)
// navigationController?.popViewController(animated: true)
}
You can do by embedding your view in a navigation controller.
Here is an image showing how to do that:
Hope it helps :D
For future searches, I wanted to add that you can now use the default icon by this code:
override public func viewDidLoad() {
// create chevron image
let config = UIImage.SymbolConfiguration(pointSize: 25.0, weight: .medium, scale: .medium)
let image = UIImage(systemName: "chevron.left", withConfiguration: config)
// create back button
let backButton = UIButton(type: .custom)
backButton.addTarget(self, action: #selector(self.close(_:)), for: .touchUpInside)
backButton.setImage(image, for: .normal)
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
#IBAction func close(_ sender: UIButton) {
self.navigationController?.dismiss(animated: true, completion: nil)
}
The first answer works great however the image is a bit too big so use the preview and scale it down to width:13 and height: 22, also set its rendering mode to .alwaysTemplate and change the UIButton's tint to white, while also adding two spaces before the string : " Back". This will result in something that is quiet similar to the navigation bar back button, the image could be better in terms of size and placement.
Edited code:
func addBackButton() {
let backButtonImage = UIImage(named: "BackButton.png")?.withRenderingMode(.alwaysTemplate)
let backButton = UIButton(type: .custom)
backButton.setImage(backButtonImage, for: .normal)
backButton.tintColor = .white
backButton.setTitle(" Back", for: .normal)
backButton.setTitleColor(.white, for: .normal)
backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
I changed one last line of code from selected answer and it works for me.
override func viewDidLoad() {
super.viewDidLoad()
addBackButton()
}
func addBackButton() {
let backButton = UIButton(type: .custom)
backButton.setImage(UIImage(named: "BackButton.png"), for: .normal) // Image can be downloaded from here below link
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal) // You can change the TitleColor
backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
#IBAction func backAction(_ sender: UIButton) {
let _ = self.dismiss(animated: true, completion: nil)
}
Swift 5
override func viewDidLoad() {
super.viewDidLoad()
let backbutton = UIButton(type: .custom)
backbutton.setImage(UIImage(named: "BackButton.png"), for: .normal) // Image can be downloaded from here below link
backbutton.setTitle("Back", for: .normal)
backbutton.setTitleColor(backbutton.tintColor, for: .normal) // You can change the TitleColor
backbutton.addTarget(self, action: Selector(("backAction")), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}
func backAction() -> Void {
self.navigationController?.popViewController(animated: true)
}

How to create a custom UIButton with same Buttonsystemitem click effect

I'm creating a custom button item for my navigation bar, and I want it to have the same effect as the button system item ( Slow fade out )
Here is my code :
var button_user : UIImage = UIImage(named:"user.png")!
let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
button.setImage(UIImage(named: "user.png"), forState: UIControlState.Normal)
button.setImage(UIImage(named: "user_clicked.png"), forState: UIControlState.Highlighted)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 28, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
You should change UIButtonType.Custom to UIButtonType.System for that:
let button = UIButton(type: UIButtonType.System)
button.setImage(UIImage(named: "user.png"), forState: UIControlState.Normal)
button.setImage(UIImage(named: "user_clicked.png"), forState: UIControlState.Highlighted)
button.addTarget(self, action: nil, forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 28, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.rightBarButtonItem = barButton
Edit
Updated for Swift 2.0
button.addTarget(self, action: "buttonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
func buttonClicked(sender:UIButton)
{
dispatch_async(dispatch_get_main_queue(), {
if isHighLighted == false{
sender.highlighted = true;
isHighLighted = true
}else{
sender.highlighted = false;
isHighLighted = false
}
});
}
OR
button.setShowsTouchWhenHighlighted(true)
Edit: You should use UIBarButtonItem instead of UIButton in the navigation bar. That will do.
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "backbutton"), style: UIBarButtonItemStyle.Plain, target: self, action: "buttonClicked:")
And also add the method
func buttonClicked(sender: AnyObject){
//Enter your code here
}
add more "images" for the UIControlState accordingly

UIBarButtonItem in navigation bar programmatically?

I've been looking around for this solution for a while but haven't got any.
e.g one solution is
self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Stop, target: self, action: nil), animated: true)
This code will add a button with "stop" image. Just like this, there are other solutions with "search, "refresh" etc. But what if I want to add a button programmatically with the image I want?
Custom button image without setting button frame:
You can use init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?) to initializes a new item using the specified image and other properties.
let button1 = UIBarButtonItem(image: UIImage(named: "imagename"), style: .plain, target: self, action: Selector("action")) // action:#selector(Class.MethodName) for swift 3
self.navigationItem.rightBarButtonItem = button1
Check this Apple Doc. reference
UIBarButtonItem with custom button image using button frame
FOR Swift 3.0
let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)
FOR Swift 2.0 and older
let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
//.... Set Right/Left Bar Button item
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
Or simply use init(customView:) like
let rightBarButton = UIBarButtonItem(customView: btnName)
self.navigationItem.rightBarButtonItem = rightBarButton
For System UIBarButtonItem
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera
For set more then 1 items use rightBarButtonItems or for left side leftBarButtonItems
let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
let item1 = UIBarButtonItem()
item1.customView = btn1
let btn2 = UIButton()
btn2.setImage(UIImage(named: "img2"), forState: .Normal)
btn2.frame = CGRectMake(0, 0, 30, 30)
btn2.addTarget(self, action: Selector("action2:"), forControlEvents: .TouchUpInside)
let item2 = UIBarButtonItem()
item2.customView = btn2
self.navigationItem.rightBarButtonItems = [item1,item2]
Using setLeftBarButtonItem or setRightBarButtonItem
let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: btn1), animated: true);
For swift >= 2.2 action should be #selector(Class.MethodName) ... for e.g. btnName.addTarget(self, action: #selector(Class.MethodName), forControlEvents: .TouchUpInside)
It's much easier with Swift 4 or Swift 4.2
inside your ViewDidLoad method, define your button and add it to the navigation bar.
override func viewDidLoad() {
super.viewDidLoad()
let logoutBarButtonItem = UIBarButtonItem(title: "Logout", style: .done, target: self, action: #selector(logoutUser))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
}
then you need to define the function that you mentioned inside action parameter as below
#objc func logoutUser(){
print("clicked")
}
You need to add the #objc prefix as it's still making use of the legacy stuff (Objective C).
Just setup UIBarButtonItem with customView
For example:
var leftNavBarButton = UIBarButtonItem(customView:yourButton)
self.navigationItem.leftBarButtonItem = leftNavBarButton
or use setFunction:
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: yourButton), animated: true);
I just stumbled upon this question and here is an update for Swift 3 and iOS 10:
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: nil)
self.navigationItem.rightBarButtonItem = testUIBarButtonItem
It is definitely much faster than creating the UIButton with all the properties and then subsequently adding the customView to the UIBarButtonItem.
And if you want to change the color of the image from the default blue to e.g. white, you can always change the tint color:
test.tintColor = UIColor.white()
PS You should obviously change the selector etc. for your app :)
In Swift 3.0+, UIBarButtonItem programmatically set up as follows:
override func viewDidLoad() {
super.viewDidLoad()
let testUIBarButtonItem = UIBarButtonItem(image: UIImage(named: "test.png"), style: .plain, target: self, action: #selector(self.clickButton))
self.navigationItem.rightBarButtonItem = testUIBarButtonItem
}
#objc func clickButton(){
print("button click")
}
FOR Swift 5+
let searchBarButtonItem = UIBarButtonItem(image: UIImage(named: "searchIcon"), style: .plain, target: self, action: #selector(onSearchButtonClicked))
self.navigationItem.rightBarButtonItem = searchBarButtonItem
#objc func onSearchButtonClicked(_ sender: Any){
print("SearchButtonClicked")
}
Setting LeftBarButton with Original Image.
let menuButton = UIBarButtonItem(image: UIImage(named: "imagename").withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(classname.functionname))
self.navigationItem.leftBarButtonItem = menuButton
iOS 11
Setting a custom button using constraint:
let buttonWidth = CGFloat(30)
let buttonHeight = CGFloat(30)
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "img name"), for: .normal)
button.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside)
button.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
button.heightAnchor.constraint(equalToConstant: buttonHeight).isActive = true
self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(customView: button)
I have same issue and I have read answers in another topic then I solve another similar way. I do not know which is more effective.
similar issue
//play button
#IBAction func startIt(sender: AnyObject) {
startThrough();
};
//play button
func startThrough() {
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true);
let pauseButton = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "pauseIt");
self.toolBarIt.items?.removeLast();
self.toolBarIt.items?.append( pauseButton );
}
func pauseIt() {
timer.invalidate();
let play = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "startThrough");
self.toolBarIt.items?.removeLast();
self.toolBarIt.items?.append( play );
}
This is a crazy thing of apple. When you say self.navigationItem.rightBarButtonItem.title then it will say nil while on the GUI it shows Edit or Save. Fresher likes me will take a lot of time to debug this behavior.
There is a requirement that the Item will show Edit in the firt load then user taps on it It will change to Save title. To archive this, i did as below.
//view did load will say Edit title
private func loadRightBarItem() {
let logoutBarButtonItem = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(handleEditBtn))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
}
// tap Edit item will change to Save title
#objc private func handleEditBtn() {
print("clicked on Edit btn")
let logoutBarButtonItem = UIBarButtonItem(title: "Save", style: .done, target: self, action: #selector(handleSaveBtn))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
blockEditTable(isBlock: false)
}
//tap Save item will display Edit title
#objc private func handleSaveBtn(){
print("clicked on Save btn")
let logoutBarButtonItem = UIBarButtonItem(title: "Edit", style: .done, target: self, action: #selector(handleEditBtn))
self.navigationItem.rightBarButtonItem = logoutBarButtonItem
saveInvitation()
blockEditTable(isBlock: true)
}
addition to the above you may use the following for ios14 and above
if #available(iOS 14.0, *) {
let closeAction = UIAction(handler: { [weak self] _ in
//perform action here
})
let closeBarButtonItem = UIBarButtonItem(systemItem: .close, primaryAction: closeAction, menu: nil)
navigationItem.rightBarButtonItem = closeBarButtonItem
}
func viewDidLoad(){
let homeBtn: UIButton = UIButton(type: UIButtonType.custom)
homeBtn.setImage(UIImage(named: "Home.png"), for: [])
homeBtn.addTarget(self, action: #selector(homeAction), for: UIControlEvents.touchUpInside)
homeBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let homeButton = UIBarButtonItem(customView: homeBtn)
let backBtn: UIButton = UIButton(type: UIButtonType.custom)
backBtn.setImage(UIImage(named: "back.png"), for: [])
backBtn.addTarget(self, action: #selector(backAction), for: UIControlEvents.touchUpInside)
backBtn.frame = CGRect(x: -10, y: 0, width: 30, height: 30)
let backButton = UIBarButtonItem(customView: backBtn)
self.navigationItem.setLeftBarButtonItems([backButton,homeButton], animated: true)
}
}

Resources