如何更改iOS 13导航栏中的后退按钮图像?

时间:2019-09-11 08:03:35

标签: ios swift uikit uinavigationbar ios13

On the right, the first time and on the left, all the other times

你们好,,我是新来的,所以我希望这是这里的事情!

众所周知,iOS 13引入了UI更改。我们有一个正在生产中的应用程序,最近我醒了(也许有点晚了哈哈),当我在新近更新的iOS 13设备上编译并启动它时,我才意识到需要做一些工作!我通过不启用它来处理黑暗模式,我处理了模态,但是似乎无法像iOS 12那样做一件事,这是我的导航栏UI。

我们使用了自定义的后退按钮图片,经过几个小时的搏斗,我终于成功了,但除第一次外,每次都可以。第一次我将始终使用默认图标,然后回到同一控制器时就可以了。

这里是一张照片(在问题的开头),因此您可以了解我的代码! 我知道可以通过«whenContained»将Appearance用于特定的VC,但由于所有内容都在导航控制器中,因此我似乎无法弄清楚。

fileprivate func navigationBarWithBackgroundColor(_ backgroundColor: UIColor, TintColor tintColor: UIColor, displayBackButtonIfNeeded: Bool, BackImage imageName:String, displayShadowBar: Bool = false) {

        let backButtonImage = UIImage(named: imageName)

        if #available(iOS 13.0, *) {

            let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = backgroundColor

            appearance.titleTextAttributes = [.foregroundColor: tintColor]
            appearance.setBackIndicatorImage(backButtonImage, transitionMaskImage: backButtonImage)
            appearance.shadowImage = displayShadowBar ? UIImage(named:"") : UIImage()

            let back = UIBarButtonItemAppearance()
            // hide back button text
            back.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
            appearance.backButtonAppearance = back

            navigationController?.navigationBar.tintColor = tintColor
            navigationController?.navigationBar.standardAppearance = appearance
            navigationController?.navigationBar.compactAppearance = appearance
            navigationController?.navigationBar.scrollEdgeAppearance = appearance

        } else {
            if displayBackButtonIfNeeded {
                self.navigationController?.navigationBar.backIndicatorImage = backButtonImage
                self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonImage
                self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)

            } else {
                self.navigationItem.setHidesBackButton(true, animated: false)
            }

            self.navigationController?.navigationBar.barTintColor = backgroundColor
            self.navigationController?.navigationBar.tintColor = tintColor
            self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:""), for: UIBarMetrics.default)
            self.navigationController?.navigationBar.shadowImage = displayShadowBar ? UIImage(named:"") : UIImage()
        }
    }

我基本上在这里疯了,我想我想念的东西很明显,所以如果你们有除苹果文档以外的任何提示或线索,请随时分享:)

提前感谢!

1 个答案:

答案 0 :(得分:2)

在iOS 13中,您只能通过功能

设置后退按钮图像和过渡遮罩图像
func setBackIndicatorImage(UIImage?, transitionMaskImage: UIImage?)

这是一个示例

standartAppearence.setBackIndicatorImage(#imageLiteral(resourceName: "backButton"), transitionMaskImage: #imageLiteral(resourceName: "backButton"))