删除后退按钮文本

Remove the back button text

目前正在尝试删除后退按钮上前一个 VC 的标题。以下 apple's documentation 我用过:

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

但是后退按钮标题并没有消失!控制台说它是空的 (""),但它仍然清楚地显示给用户。

我不想使用 hack,例如将上一个 VC 的前一个标题设置为“”,但目前似乎没有其他任何工作。有什么建议吗?

你可以用 UIButton 来初始化它

let button = UIButton()
let item = UIBarButtonItem(customView: button)
navigationItem.backBarButtonItem = item

将此函数作为 UINavigationControllerextension 试试:

func setBackButtonTitle(_ title: String) {
    if self.navigationBar.topItem != nil {
        let leftBarButtonItem: UIBarButtonItem = UIBarButtonItem(title: title, style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.backButtonAction))
        self.navigationBar.topItem?.backBarButtonItem = leftBarButtonItem
    } else {
        if self.navigationBar.backItem != nil {
            self.navigationBar.backItem?.title = title
        }
    }
}

试试这个:

self.navigationController?.navigationBar.topItem?.backBarButtonItem = backBarButtton

当其他控制器推到当前控制器上方时,您的代码会更改后退按钮的外观。但是你需要修改当前控制器上的后退按钮。

查看有关 backBarButtonItem 的文档:

When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.