在iOS 13上隐藏后退按钮标题

时间:2019-08-29 11:46:55

标签: ios

我们的应用程序的部署目标为iOS10。我们使用以下代码隐藏了所有后退按钮标题(针对整个应用程序)

let attributes: [NSAttributedString.Key : Any] = [
    .font : UIFont.systemFont(ofSize: 0.001),
    .foregroundColor: UIColor.clear
]

let barButtonItemAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
barButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
barButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

这一直持续到iOS 13,后退按钮标题不再隐藏。

我看了有关新外观API的WWDC 2019视频,但据我所知,它只告诉我如何在单个导航栏中(而不是整个应用程序)使用新API。

我该如何实现?

4 个答案:

答案 0 :(得分:3)

添加父级(堆栈中的上一个)控制器的viewDidLoad()方法。

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

在下一个屏幕中,将仅显示箭头图像。

或者您可以将此代码放在pushViewController方法之前,例如

func presentNextScreen(_ controller: UIViewController){
   // Suppress title in the back button on the next screen.
   self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
   self.navigationController?.pushViewController(controller, animated: true) 
}

答案 1 :(得分:0)

解决方法: 创建一个基类:BaseViewController。

class BaseViewController: UIViewController {

func hideBackButton() {
    self.navigationItem.leftBarButtonItem = nil
}

func hideCloseButton() {
    self.navigationItem.rightBarButtonItem = nil
}

override public func viewDidLoad() {
    super.viewDidLoad()
    addNavigationButtons()
}

func addNavigationButtons() {
    let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(goBack))
    self.navigationItem.leftBarButtonItem = backButton

    let closeButton = UIBarButtonItem(image: UIImage(named: "close"), style: .plain, target: self, action: #selector(closeTheFlow))
    self.navigationItem.rightBarButtonItem = closeButton
}

@objc func goBack() {
    //Go Back
}

@objc func closeTheFlow() {
    //Close
}

使每个ViewController都是BaseViewController的子类。 您可以在整个应用程序中使用左右按钮来做任何事情。

答案 2 :(得分:0)

我将标题隐藏起来。一个把戏

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -UIScreen.main.bounds.size.width, vertical: 0), for: UIBarMetrics.default)

答案 3 :(得分:0)

对我来说,将标题隐藏在“后退”按钮中的最简单方法是配置UINavigationBar的外观

if #available(iOS 13.0, *) {
            let standartAppearence = UINavigationBarAppearance()
            standartAppearence.configureWithDefaultBackground()

            let backButtonAppearence = UIBarButtonItemAppearance()
            let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
            backButtonAppearence.normal.titleTextAttributes = titleTextAttributes
            backButtonAppearence.highlighted.titleTextAttributes = titleTextAttributes
            standartAppearence.backButtonAppearance = backButtonAppearence

            UINavigationBar.appearance().standardAppearance = standartAppearence
            UINavigationBar.appearance().compactAppearance = standartAppearence
            UINavigationBar.appearance().scrollEdgeAppearance = standartAppearence
        }

此外,您可以在此处添加导航栏的任何其他配置