简体   繁体   中英

Customised navigation bar back button title

I want customised navigation bar back button title with action.

My Image :

在此处输入图片说明

I need back button like this. Here i added text successfully, but not back arrow. Here my code has action for back button.

This is my code

    //Custom barButtonItem with custom alert function
    self.navigationItem.hidesBackButton = true
    let newBackButton = UIBarButtonItem(title: "< Dialer", style: .plain, target: self, action: #selector(back(sender:)))
    self.navigationItem.leftBarButtonItem = newBackButton

When i add above code it's getting like this. 在此处输入图片说明

func backButtonConfiguration(_ title : String, backImageName : String) {
    let backImage : UIImage = UIImage(named: backImageName)!
    self.navigationBar.backIndicatorImage = backImage.withRenderingMode(.alwaysTemplate)
    self.navigationBar.backIndicatorTransitionMaskImage = backImage
    self.navigationBar.backItem?.title = title
}

Update

if we need custom action for back button then we have to add custom BarButtonItem. And if we need bar back button look same as built in back button then we can create custom view and use it it as a bar button item

  weak var customView : UIView!

  lazy var leftBarButtonView : UIBarButtonItem! = {
    let btnBack = UIBarButtonItem(customView: customView)
    let gesture = UITapGestureRecognizer(target: self, action: #selector(btnButtonClicked(_:)))
    gesture.numberOfTapsRequired = 1
    self.customView.addGestureRecognizer(gesture)
    self.customView.isUserInteractionEnabled = true
    return btnBack
  }()

  @objc func btnButtonClicked(_ gesture : UITapGestureRecognizer) {
    self.navigationController?.popViewController(animated: true)
  }

 self.navigationItem.leftBarButtonItems = [leftBarButtonView]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM