4

I am trying to make a custom back button using this code:

let back = UIImage(named: "header_backarrow")
let backView = UIImageView(image: back)
let backItem = UIBarButtonItem(customView: backView)
navigationItem.leftBarButtonItem = backItem

I want the navigation item to perform this code:

func dismissManual() {
    dismiss(animated: true, completion: nil)
}

I have tried many things like following this Stack Overflow post: Execute action when back bar button of UINavigationController is pressed

I have also tried making it a navigationItem.backBarButtonItem; however, nothing seems to work. Some things show the correct custom image, but do not work as a button; on the other hand, some work as a button, but do not show the correct image.

Does anybody know how I can show the correct image and make the item work as a button? Thanks.

Patrick Haertel
  • 309
  • 4
  • 15

4 Answers4

1

Do it as follows:

override func viewDidLoad() {
        super.viewDidLoad()

        let back = UIImage(named: "header_backarrow")
        let backView = UIImageView(image: back)
        backView.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(dismissManual))
        backView.addGestureRecognizer(tap)
        let backItem = UIBarButtonItem(customView: backView)
        navigationItem.leftBarButtonItem = backItem
    }

    @objc func dismissManual() {
        print("print----")
//        dismiss(animated: true, completion: nil)
    }

Add gesture to backView it will work! It's similiar to this question IOS - Swift - adding target and action to BarButtonItem's customView

bobo pei
  • 52
  • 1
  • 10
  • Your answer was by far the simplest and easiest to integrate. Thank you for your help. – Patrick Haertel Jun 13 '18 at 16:40
  • @PatrickHaertel You are of course free to choose which ever answer you want but how is this the simplest and easiest? This is far more complicate and error prone that other solutions. Yes, this works, but it is not the simplest at all because other solutions have no need for the image view or the tap gesture. – rmaddy Jun 13 '18 at 22:46
0

Swift 4.1

The issue is that UIImage does not have tap recognition. You will have to add a tap gesture recognizer to your backView.

    lazy var singleTap: UITapGestureRecognizer = {
    let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
    singleTap.numberOfTapsRequired = 1
    return singleTap
}()

// Actions
@objc func tapDetected() {
    dismiss(animated: true, completion: nil)
}

If you show your code, with some screenshots I can give more help if this doesn't solve the issue.

llamacorn
  • 551
  • 4
  • 23
0

You are creating your UIBarButtonItem incorrectly. You do not need the image view.

Do it as follows:

let back = UIImage(named: "header_backarrow")
let backItem = UIBarButtonItem(image: back, style: .plain, target: self, action: #selector(dismissManual))
navigationItem.leftBarButtonItem = backItem

@objc func dismissManual() {
    dismiss(animated: true, completion: nil)
}

Note that the function must be marked with @objc.

Depending on your image and how you want it displayed, you may need to create the image as follows:

let back = UIImage(named: "header_backarrow").withRenderingMode(.alwaysOriginal)

Another option is to create a UIButton with the image and setup to call your dismissManual function. Create the UIBarButtonItem with the button as the custom view.

But it's easier to create a standard UIBarButtonItem when all you have is a simple image or a simple string.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0
let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
                                            style: .plain,
                                            target: self,
                                            action: #selector(menuButtonTapped))

// Adding button to navigation bar (rightBarButtonItem or leftBarButtonItem)
self.navigationItem.rightBarButtonItem = barButtonItem

 // Private action
@objc fileprivate func menuButtonTapped() { // body method here }

Check out this, it may help Thanks.

Praveen Gowlikar
  • 215
  • 2
  • 16