将文本设为 UINavigationController 后退按钮

Make text as UINavigationController back button

我需要做这样的导航栏

但我不知道如何将 backBarButton 设置为文本。我应该创建 xib 来替换我的导航栏还是我可以通过编程来完成

在 viewDidLoad 中设置自定义按钮 navBar:

let button = UIButton(type: .custom)
    //Set the image
    button.setImage(UIImage(systemName: "chevron.backward"), for: .normal)
    //Set the title
    button.setTitle("Yourtitle", for: .normal)
    //Add target
    button.addTarget(self, action: #selector(callMethod), for: .touchUpInside) //call button tap action
    // Add insets padding
    let spacing: CGFloat = -10 // the amount of spacing to appear between image and title
    button.imageEdgeInsets = UIEdgeInsets(top: 0, left: spacing, bottom: 0, right: 0)
    button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) // customize your button titleEdgeInsets if you want
    //Create bar button
    let barButton = UIBarButtonItem(customView: button)
    navigationItem.leftBarButtonItem = barButton

之后创建用于点击按钮的函数:

@objc fileprivate func callMethod() {
    print("Your code here")
}