2

在我的应用程序中,我想删除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

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

谢谢

4

3 回答 3

2

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

我通过扩展做了这样的事情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()

如果有更好的方法来为整个应用程序执行此操作,请告诉我。

于 2019-09-26T09:33:41.907 回答
1

我添加了 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
    
    }
于 2021-12-21T11:50:50.913 回答
0

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

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

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

于 2019-10-07T23:30:08.583 回答