how to programmatically set action for barButtonItem in swift 3?

swift add navigation bar button programmatically
uibutton as uibarbuttonitem swift
swift add navigation bar programmatically
uibarbuttonitem action not working
how to add custom image in navigation bar button item swift
uibarbuttonitem programmatically objective c
navigationitem.rightbarbuttonitem not showing swift
barbuttonsystemitem

Here is what I used previously,

var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action: Selector("menuButtonTapped:"))

But there is some syntax changes for Swift 3.

ex:-

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))

Create UIBarButtonItem Programmatically, How to set left side bar button item(leftBarButtonItem); How to set target action on UIBarButtonItem to call custom function when button is tapped. UIBarButtonItem in navigation bar programmatically? In swift 3 for action in the bar button: Set UIBarButtonItem target programmatically to previous view

Summarize the mostly used method in Swift 3 for adding action to a barButton.

  1. Barbutton with text

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    
  2. BarButton with your own image

    navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"add"), style: .plain, target: self, action: #selector(addTapped))
    
  3. BarButton with system image

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    

How to add a bar button to a navigation bar, How to add multiple UIBarButtonItem to a navigation bar using let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action:  Questions: I am trying to set an Image for bar button Item for that I have an image like: with resolution 30 * 30 but while I assign this Image to Bar button Its looks like: I have assigned image this way : and If I try this way like making an IBOutlet for the

If anyone is using customView:

barButtonItem.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onBarButtonItemClicked)))

How to add multiple UIBarButtonItem to a , But there is some syntax changes for Swift 3. Thanks in advance. Answers: ex:- navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add"  In general, you should specify a value for the normal state to be used by other states which don’t have a custom value set. Similarly, when a property is dependent on the bar metrics (on iPhone, in landscape orientation bars have a different height from standard), you should specify a value of UIBar Metrics.default.

You just need to change your selector syntax as of from Swift 3 you need to specify the first parameter name of method in your function call so change your selector like this.

#selector(menuButtonTapped(sender:))

And your method should be like this.

func menuButtonTapped(sender: UIBarButtonItem) {

}

UIBarButtonItem: Change title at every tap - Using Swift, I want to make a UIBarButtonItem that changes title by tap. sendAction(menu.​action!, to: menu.target, from: nil, for: nil) //set webview let  How to set local alerts using UNNotificationCenter; How to render shadows using NSShadow and setShadow() About the Swift Knowledge Base. This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

One line of code on Swift 3 for iOS 10.1:

navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: nil)

How to connect bar button items to IBAction? : iOSProgramming, 3 years ago You could just add the bar button items in programmatically in your view Swift: self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "​Button", style: . style:UIBarButtonItemStylePlain target:self action:@selector(​someFunction)]; If you set the classes properly both in code and in InterfaceBuilder. The Complete iOS 11 & Swift Developer Course - Build 20 Apps Use Xcode 9 & Swift 4 to make real apps like Uber and Instagram, with CoreML & ARKit. Includes AWS Credit and much more.

UIBarButtonItem, iOS 2.0+; Mac Catalyst 13.0+; tvOS 9.0+. Framework You typically use Interface Builder to create and configure bar button items. The selector defining the action message to send to the target object when the user taps this bar button item. how to programmatically set action for barButtonItem in swift 3? Ask Question Asked 3 years, 6 months ago. How to add constraints programmatically using Swift. 0.

Insert right button image in UINavigationController with swift 3, Here is what I used previously, var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action:  Alternatively, you can also set the barButtonItem property. UIKit needs this information to specify the location for the popover. UIKit needs this information to specify the location for the popover. Unfortunately, UIKit places the arrow in the upper left corner of the source view.

Insert right button image in UINavigationController with swift 3 - ios. UIBarButtonItem(image: logoBPT, style: .plain, target: nil, action: nil) self. and If I try this way like making an IBOutlet for the button and set Image programatically form this  This is where the backBarButtonItem property comes in: set this to an instance of UIBarButtonItem to have UIKit create a back button title of your choosing. You don’t need to provide anything for the target or action parameters of your button, because even with a custom title it’s still just a back button.

Comments
  • Thank you! It's saved my time.
  • I would click on the up arrow but it's at 69 so we'll leave it for now.
  • @JackDerbis 😂😂😂
  • This is what will work if you need to add the gesture recognizer to a bar button item when using the customView initializer. As far as I've found only this method signature will work (adding the gesture to the view itself/to the bar button/anything else didn't work for me).
  • From swift 3, I have to specify first parameter, but I don't need to do this.. anyway, thanks
  • @Bhanupriya Yes you can but for that you need to use _ with selector #selector(menuButtonTapped(_:)) and also need to add _ as first parameter to your method like func menuButtonTapped(_ sender: UIBarButtonItem) { either one of the option you have to use other wise it will not work.
  • @Bhanupriya Are you getting what i have tell you about selector in above comment?
  • No, I don't need to. what I need is as follows: var barButtonItem = UIBarButtonItem(image: backImgs, style: .plain, target: self, action: #selector(menuButtonTapped))
  • @Bhanupriya then why you are previously try Selector("menuButtonTapped:") here : means method contain one parameter that is the reason i have given answer this way.
  • Great. It's also for Swift 4 and lower versions.