How to programmatically set action for barButtonItem in swift 3?
ex:- navigationItem.rightBarButtonItem = UIBarButtonItem(title: “Add”, style: .plain, target: self, action: #selector(addTapped))
ex:- navigationItem.rightBarButtonItem = UIBarButtonItem(title: “Add”, style: .plain, target: self, action: #selector(addTapped))
I spent an evening trying to figure this out as well. You were very close to the solution. The trick is to instantiate the UIImage with the rendering mode. Instead of doing: UIImage *image = [UIImage imageNamed:@”myImage.png”]; image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; do this: UIImage *image = [[UIImage imageNamed:@”myImage.png”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; and it works! In my case, … Read more
And for iOS 7+ you do the following: Objective-C UIImage *image = [[UIImage imageNamed:@”myImage.png”] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(YOUR_METHOD:)]; Swift 2.3 let image = UIImage(named: “myImage”)?.imageWithRenderingMode(.AlwaysOriginal) let button = UIBarButtonItem(image: image, style: .Plain, target: self, action: #selector(YOUR_METHOD(_:))) Swift 3.0 let image = UIImage(named: “myImage”)?.withRenderingMode(.alwaysOriginal) let button = UIBarButtonItem(image: image, style: … Read more
You can do something like that: let yourBackImage = UIImage(named: “back_button_image”) self.navigationController?.navigationBar.backIndicatorImage = yourBackImage self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage self.navigationController?.navigationBar.backItem?.title = “Custom” Your image will only have one color though
Just set the UIBarButtonItem’s target and action properties directly.
This code creates a back button for UINavigationBar with image background and custom position. The trick is to create an intermediate view and modify its bounds. Swift 5 let menuBtn = UIButton(type: .custom) let backBtnImage = UIImage(named: “menu”) menuBtn.setBackgroundImage(backBtnImage, for: .normal) menuBtn.addTarget(self, action: #selector(showMenuTapped), for: .touchUpInside) menuBtn.frame = CGRect(x: 0, y: 0, width: 45, height: … Read more
I have achieved that programatically with this code: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //create a new button let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton //set image for button button.setImage(UIImage(named: “fb.png”), forState: UIControlState.Normal) //add function for button button.addTarget(self, action: “fbButtonPressed”, forControlEvents: UIControlEvents.TouchUpInside) //set frame button.frame = CGRectMake(0, 0, 53, 31) … Read more
EDIT: These problems appear to be fixed as of iOS 7.1 / Xcode 5.1.1. (Possibly earlier, as I haven’t been able to test all versions. Definitely after iOS 7.0, since I tested that one.) When you create a popover segue from a UIBarButtonItem, the segue makes sure that tapping the popover again hides the popover … Read more
Works until iOS11! You can use negative flexible spaces and rightBarButtonItems property instead of rightBarButtonItem: UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; spacer.width = -10; // for example shift right bar button to the right self.navigationItem.rightBarButtonItems = @[spacer, yourBarButton];
Use this in swift: override func viewDidLoad() { super.viewDidLoad() let editImage = UIImage(named: “plus”)! let searchImage = UIImage(named: “search”)! let editButton = UIBarButtonItem(image: editImage, style: .Plain, target: self, action: “didTapEditButton:”) let searchButton = UIBarButtonItem(image: searchImage, style: .Plain, target: self, action: “didTapSearchButton:”) navigationItem.rightBarButtonItems = [editButton, searchButton] } Write the action functions like this: func didTapEditButton(sender: AnyObject){ … Read more