【问题标题】:UINavigationBar Back Button Title can't be removed using navigation bar appearance for iOS 13UINavigationBar 后退按钮标题无法使用 iOS 13 的导航栏外观删除
【发布时间】:2020-01-26 12:58:18
【问题描述】:

在我的应用程序中,我想删除 UINavigationBar 后退按钮标题。 我已经完成了以下代码

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     // do other staffs 
     initNavigationBar()

     return true 

}

private func initNavigationBar() {

        let appearance = UINavigationBar.appearance()
        appearance.barTintColor = GLOBAL_TINT_COLOR // a globally declared colour 
        appearance.tintColor = .white
        appearance.barStyle = .black

        if #available(iOS 13.0, *) {
            let backButtonAppearance = UIBarButtonItemAppearance()
            backButtonAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
            appearance.standardAppearance.backButtonAppearance = backButtonAppearance
            appearance.compactAppearance?.backButtonAppearance = backButtonAppearance
            appearance.scrollEdgeAppearance?.backButtonAppearance = backButtonAppearance
        } else {


            // Hide navigation bar back button items

            UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)

            UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .highlighted)
        }

}

但是,此代码始终适用于 iOS 10-12,但不适用于 iOS 13。我错过了什么吗?

在其他情况下,我找到了很多关于该主题的答案,但没有找到iOS 13的解决方案

我从不想使用set back button title as an empty string,而不是使用外观来修复它。

谢谢

【问题讨论】:

    标签: ios swift uinavigationbarappearance


    【解决方案1】:

    几周前我遇到了类似的问题。我没有找到针对整个应用程序全局执行此操作的方法,因此我求助于自定义每个导航控制器(幸好并不多)。

    我通过扩展 UINavigationController 做了类似的事情:

    @available(iOS 13, *)
    func hideBackButton() {
        let appearance = self.navigationBar.standardAppearance
    
        let hideBackButtonTitleAttributes: [NSAttributedString.Key: Any] = [
            .foregroundColor: UIColor.clear
        ]
    
        let normalBackButton = appearance.backButtonAppearance.normal
        let highlightedBackButton = appearance.backButtonAppearance.highlighted
    
        normalBackButton.titleTextAttributes = hideBackButtonTitleAttributes
        highlightedBackButton.titleTextAttributes = hideBackButtonTitleAttributes
    
        navigationBar.standardAppearance = appearance
    }
    

    然后我像这样使用hideBackButton 方法:

    navigationController?.hideBackButton()
    

    如果对整个应用程序有更好的方法,请告诉我。

    【讨论】:

    • 是的,我不想到处写navigationController?.hideBackButton(),如果找到更好的解决方案我会回复你。谢谢!
    • @AnkurLahiry 你可以subClass UINavigationController,把这个自定义放在里面并使用那个class。 IMO,最不推荐使用外观。
    【解决方案2】:

    您可以将您的后备项目设置为没有这样的标题:

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil];
    

    请注意,这会影响将其他内容推送到导航堆栈时显示的内容。如果您将视图控制器 A 设置为导航控制器的根并像这样设置 A 的后项,则当您将视图控制器 B 推入堆栈时,您会看到它。设置 B 的后退项不会影响 B 可见时您在后退项中看到的内容。

    【讨论】:

      【解决方案3】:

      我已经添加了 ios15 移除 BackBar 按钮标题和设置导航:

          if #available(iOS 15, *) {
              let appearance = UINavigationBarAppearance()
              let pargraphStyle = NSMutableParagraphStyle()
              pargraphStyle.alignment = .center
              let fontApply = UIFont.bold(size: 18)
              UINavigationBar.appearance().tintColor = UIColor.themeColor
              appearance.configureWithOpaqueBackground()
              appearance.backgroundColor = UIColor.black
              appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.themeColor, NSAttributedString.Key.font: fontApply, NSAttributedString.Key.paragraphStyle: pargraphStyle]
              appearance.shadowImage = nil
              appearance.shadowColor = .clear
              UINavigationBar.appearance().barStyle = .default
              UINavigationBar.appearance().tintColor = UIColor.themeColor
              UINavigationBar.appearance().isTranslucent = false
              UINavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "BackIcon")
              UINavigationBar.appearance().backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "BackIcon")
              UINavigationBar.appearance().standardAppearance = appearance
              UINavigationBar.appearance().scrollEdgeAppearance = appearance
              
              // Set Back Bar Button Appearance
              let appearanceBackButton = UIBarButtonItemAppearance()
      
              let hideBackButtonTitleAttributes: [NSAttributedString.Key: Any] = [
                  .foregroundColor: UIColor.clear
              ]
      
              let normalBackButton = appearance.backButtonAppearance.normal
              let highlightedBackButton = appearance.backButtonAppearance.highlighted
      
              normalBackButton.titleTextAttributes = hideBackButtonTitleAttributes
              highlightedBackButton.titleTextAttributes = hideBackButtonTitleAttributes
              appearance.buttonAppearance = appearanceBackButton
          
          }
      

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center