简体   繁体   English

导航控制器中的后退按钮不会更改为自定义图像

[英]Back button in navigation controller wont change to custom image

I have two VC, the first of which is embedded in a NC.我有两个 VC,第一个嵌入在 NC 中。 My second VC is presented using a push segue from my first VC.我的第二个 VC 是使用我的第一个 VC 中的 push segue 呈现的。 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.在我的第二个 VC 中,我试图将后退按钮更改为我拥有的自定义图像,但它仍然使用导航栏中的默认图像和文本。

viewDidLoad() in my second VC: viewDidLoad() 在我的第二个 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?为什么以下不更改我的第二个 VC 中的后退按钮?

The culprit is line that sets the backButtonItem , replacing anything you've done to the back button with a new back button.罪魁祸首是设置backButtonItem ,用新的后退按钮替换您对后退按钮所做的任何事情。 Just remove:只需删除:

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

Don't set the title to "" , if you do not need a title set the title to nil .不要将标题设置为"" ,如果您不需要标题,请将标题设置为nil Can't comment, not enough reputation, sorry.无法评论,声望不够,抱歉。 See from the docs:从文档中看到:

"The item's title. If nil a title is not displayed." “项目的标题。如果为零,则不显示标题。”

You need to set it on the first view controller.您需要在第一个视图控制器上设置它。

Calling the following code on your first view controller.在您的第一个视图控制器上调用以下代码。

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()
}

More details https://sarunw.com/posts/how-to-change-back-button-image/ https://sarunw.com/posts/how-to-remove-text-from-uinavigationbar-back-button/更多详情https://sarunw.com/posts/how-to-change-back-button-image/ https://sarunw.com/posts/how-to-remove-text-from-uinavigationbar-back-button/

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

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