简体   繁体   English

iOS 导航中的自定义后退按钮

[英]custom back button in navigation in iOS

I know this question has been asked before but nothing worked for me and I had to ask it again.我知道以前有人问过这个问题,但对我没有任何帮助,我不得不再问一次。 I want an image as my back button in navigation bar, just want to change the appearance of the back button.我想要一个图像作为导航栏中的后退按钮,只想更改后退按钮的外观。 I don't want to add a button and add selectors for it.我不想添加按钮并为其添加选择器。

I tried the following code:我尝试了以下代码:

let backImage = UIImage(named: "Back_button")

        let backAppearance = UIBarButtonItem.appearance()
        backAppearance.setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)

        navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)

I also tried setting the back image and back mask using storyboard but both these approaches place a black circle on my back image.我还尝试使用 storyboard 设置背面图像和背面蒙版,但这两种方法都会在我的背面图像上放置一个黑色圆圈。

I tried setting another image as back mask by setting its alpha content equal to zero using the code but it didn't work either.我尝试通过使用代码将其 alpha 内容设置为零来将另一个图像设置为后掩码,但它也不起作用。

please help.请帮忙。

let backButton = UIBarButtonItem()
backButton.title = "Back"
backButton.image = UIImage(named: "Back_button")
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

You can do this to customize your Back button .您可以这样做来自定义您的后退按钮 And you don't have to worry about adding selectors .而且您不必担心添加选择器

This code works with Swift 5.此代码适用于 Swift 5。

let backButton: UIButton = UIButton()
backButton.setImage(UIImage(named: "back"), for: UIControl.State())
backButton.addTarget(self, action:#selector(SearchResultsViewController.onBack), for: UIControl.Event.touchUpInside)
let leftBarButtonItem = UIBarButtonItem(customView: backButton)
navigationItem.leftBarButtonItem = leftBarButtonItem

I used this code to customize the back button on only one of my views:我使用此代码仅在我的一个视图上自定义后退按钮:

    self.navigationController?.navigationBar.topItem?.backButtonTitle = ""
    let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(goBack))
    navigationItem.leftBarButtonItem = backButton

@objc func goBack() {
    self.navigationController?.popViewController(animated: true)
}

I know this question has been asked before but nothing worked for me and I had to ask it again.我知道以前有人问过这个问题,但对我没有任何帮助,我不得不再问一次。 I want an image as my back button in navigation bar, just want to change the appearance of the back button.我想要一个图像作为导航栏中的后退按钮,只想更改后退按钮的外观。 I don't want to add a button and add selectors for it.我不想添加按钮并为其添加选择器。

I tried the following code:我尝试了以下代码:

let backImage = UIImage(named: "Back_button")

        let backAppearance = UIBarButtonItem.appearance()
        backAppearance.setBackButtonBackgroundImage(backImage, for: .normal, barMetrics: .default)

        navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)

I also tried setting the back image and back mask using storyboard but both these approaches place a black circle on my back image.我还尝试使用故事板设置背景图像和背景蒙版,但这两种方法都在我的背景图像上放置了一个黑色圆圈。

I tried setting another image as back mask by setting its alpha content equal to zero using the code but it didn't work either.我尝试通过使用代码将其 alpha 内容设置为零来将另一个图像设置为背罩,但它也不起作用。

please help.请帮忙。

  • Create a custom class for define navigation bar traits创建用于定义导航栏特征的自定义类
  • Create an extension to UINavigationController for configure it创建 UINavigationController 的扩展以进行配置

import UIKit

private final class MyNavigationBarTraits {

  public var backIndicatorImage: UIImage?
  public var backIndicatorTransitionMaskImage: UIImage?

  public func apply(to navigationBar: UINavigationBar) {
    navigationBar.backIndicatorImage = backIndicatorImage
    navigationBar.backIndicatorTransitionMaskImage = backIndicatorTransitionMaskImage
  }

  public init(navigationBar: UINavigationBar) {
    backIndicatorImage = navigationBar.backIndicatorImage
    backIndicatorTransitionMaskImage = navigationBar.backIndicatorTransitionMaskImage
  }
}

public typealias Callback<T> = (_: T) -> Void

public extension UINavigationController {

  private struct AssociationKeys {
    static var navigationBarTraits = "ws_nc_navigationBarTraits"
  }

  private var navigationBarTraits: MyNavigationBarTraits? {
    get {
      return objc_getAssociatedObject(self, &AssociationKeys.navigationBarTraits) as? MyNavigationBarTraits
    }
    set {
      objc_setAssociatedObject(self, &AssociationKeys.navigationBarTraits, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }
  }

  func configureBar(block: Callback<UINavigationBar>) {
    navigationBarTraits = MyNavigationBarTraits(navigationBar: navigationBar)
    block(navigationBar)
  }

  func resetBar() {
    navigationBarTraits?.apply(to: navigationBar)
    navigationBarTraits = .none
  }
}

And then you can configure your navigation bar in your ViewController's viewWillAppear (for example tintColor )然后你可以在你的 ViewController 的viewWillAppear配置你的导航栏(例如tintColor

  public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    navigationController?.configureBar { navigationBar in
      // You can customize your navigation bar in here!
      navigationBar.tintColor = .red
    }
  }

If you want to use this customization just in one View Controller you should reset bar in your View Controller's viewWillDisappear如果您只想在一个视图控制器中使用此自定义,您应该在视图控制器的viewWillDisappear重置栏

  public override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    navigationController?.resetBar()
  }

Simply Add Below Methods in Your ViewController :只需在您的 ViewController 中添加以下方法:

func setLeftBarBackItem() {
    
    let leftBarBackItem = UIBarButtonItem(image: #imageLiteral(resourceName: "imgBack"), style: .plain, target: self, action: #selector(self.clickToBtnBackItem(_:)))
    self.navigationItem.leftBarButtonItem = leftBarBackItem
    
}

func clickToBtnBackItem(_ sender: UIBarButtonItem) {
    view.endEditing(true)
    
    _ = navigationController?.popViewController(animated: true)
    
}

func setTranspertNavigation()
{    
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.view.backgroundColor = .clear
}

Inside Your ViewController's ViewDidLoad Method, Set backButton As :在 ViewController 的 ViewDidLoad 方法中,将 backButton 设置为:

    self.navigationController?.isNavigationBarHidden = false
    AppDelegate.shared().setupNavigationBar()
    setLeftBarBackItem()
    setTranspertNavigation()
    self.title = "Title Here"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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