如何删除导航控制器后退按钮文本

时间:2018-06-14 20:08:13

标签: swift uinavigationcontroller navigationbar

在故事板中创建导航控制器并为另一个视图控制器创建一个segue时,您会自动获得带有箭头和上一页标题的后退按钮。 (见图)

Image of problem

如何删除文字但仍有箭头? 要么 如何用我自己的图像替换?

谢谢!

2 个答案:

答案 0 :(得分:3)

在您设置的viewDidLoad内,您可以使用此更改标题

let backButton = UIBarButtonItem()
backButton.title = "" //in your case it will be empty or you can put the title of your choice
self.navigationController?.navigationBar.topItem?.backBarButtonItem = backButton

这将设置您自己的图像

let backImage = UIImage(named: "backButton")
self.navigationController?.navigationBar.backIndicatorImage = backImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage

答案 1 :(得分:2)

您可以像这样实现UINavigationControllerDelegate:

 func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let item = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
        viewController.navigationItem.backBarButtonItem = item
    }

它影响下一个推送控制器backBarButtonItem标题。

修改ViewController

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource,UINavigationControllerDelegate  {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.delegate = self
    }