【问题标题】:How to remove title from UIBarButtonItem.appearance() on iOS 10.3如何从 iOS 10.3 上的 UIBarButtonItem.appearance() 中删除标题
【发布时间】:2019-12-17 05:17:10
【问题描述】:

我想对我的应用程序的后退按钮进行个性化设置,以获得一致的外观。所以我将图像设置为导航栏的后退按钮。

下面的代码在 iOS 10 以上的任何 iOS 上都可以正常工作。所以,我试图让它在 iOS 10.3.1 上工作。

这基本上是我在 AppDelegate didFinishLaunchingWithOptions 上设置的所有代码

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        applyNavigationAppearances()
        return true
    }

    private func applyNavigationAppearances() {
        let navigationAppearance = UINavigationBar.appearance()
        navigationAppearance.barTintColor = .white
        navigationAppearance.barStyle = .black
        navigationAppearance.backIndicatorImage = UIImage()
        navigationAppearance.backIndicatorTransitionMaskImage = UIImage()
        navigationAppearance.tintColor = .gray

        navigationAppearance.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.gray,
            NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 20)
        ]

        let backImage = UIImage(named: "chevron-orange-left")
        let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
        let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
        let barButtonAppearance = UIBarButtonItem.appearance()

        barButtonAppearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
        barButtonAppearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets, for: .normal, barMetrics: .default)
    }

这是我想要并在任何 10 以上的 iOS 上得到的:

这是我在 iOS 10.3.1 上得到的:

编辑1:值得说这是一个大应用程序。不涉及更改每个 ViewController 的解决方案将是理想的。

编辑2:谢谢你们的回答。但是,此处发布的所有解决方案都会使图像变短或变长,因此它们并不能完全解决我的问题。

【问题讨论】:

  • 有点用,但是图标变小了:imgur.com/a/qVXdJ6Q
  • @zero3nna 我看过这个。它没有解决我的问题。我在 iOS 10.3 上总是出现奇怪的行为
  • @TassioMarques 那里的解决方案在 iOS 12 之前运行良好,因此您可能需要更改为其他内容并将您的 VC 子类化。您无法通过 appDelegate 执行此操作,但我也没有在您的代码中看到任何试图清除默认后退按钮的内容。

标签: swift uinavigationbar uibarbuttonitem uiappearance ios10.3


【解决方案1】:

你有:

  let backImage = UIImage(named: "icon")
  let backImageUIEdgeInsets = UIEdgeInsets(top: -4, left: -8, bottom: -2, right: 8)
  let backImageWithAlignmentRectInsets = backImage?.withAlignmentRectInsets(backImageUIEdgeInsets)
  appearance.setBackButtonBackgroundVerticalPositionAdjustment(-1.0, for: .default)
  appearance.setBackButtonBackgroundImage(backImageWithAlignmentRectInsets,
                                          for: .normal,
                                          barMetrics: .default)

之后你可以添加:

 appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
                                   for: .normal)
 appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear],
                                   for: .highlighted)

我在 iPhone 7 10.3.1 上检查了这个

【讨论】:

  • 是的,我试过了,但是图像被拉长了。对于您正在使用的图像,它可以正常工作,但对于我想要的图像,它只会隐藏文本并且仍然会扭曲图标。
猜你喜欢
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2018-10-13
  • 2017-08-23
  • 2012-12-14
  • 2018-11-21
  • 1970-01-01
相关资源
最近更新 更多