Back button in navigation controller wont change to custom image

时间:2018-01-15 18:19:12

标签: ios swift uinavigationcontroller

I have two VC, the first of which is embedded in a NC. My second VC is presented using a push segue from my first VC. In my second VC, I am trying to change the back button to a custom image I have, but it is still using the default image and text in the navigation bar.

viewDidLoad() in my second VC:

override func viewDidLoad() {
    super.viewDidLoad()

    let back = UIImage(named: "back")
    self.navigationController!.navigationBar.backIndicatorImage = back
    self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = back
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

    configureView()
}

Why is the following not changing the back button in my second VC?

3 个答案:

答案 0 :(得分:1)

罪魁祸首是设置backButtonItem的行,用新的后退按钮替换你对后退按钮做的任何事情。只需删除:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

答案 1 :(得分:0)

不要将标题设置为"",如果您不需要标题,则将标题设置为nil。无法评论,没有足够的声誉,抱歉。请参阅文档:

"项目的标题。如果没有显示标题。"

答案 2 :(得分:0)

您需要在第一个视图控制器上进行设置。

在第一个视图控制器上调用以下代码。

override func viewDidLoad() {
    super.viewDidLoad()

    let back = UIImage(named: "back")
    self.navigationController!.navigationBar.backIndicatorImage = back
    self.navigationController!.navigationBar.backIndicatorTransitionMaskImage = back
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: nil, action: nil)

    configureView()
}

更多详细信息 https://sarunw.com/posts/how-to-change-back-button-image/ https://sarunw.com/posts/how-to-remove-text-from-uinavigationbar-back-button/