简体   繁体   English

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

[英]How can I change the back button image in the navigation bar for iOS 13?

On the right, the first time and on the left, all the other times在右边,第一次,在左边,所有其他时间

Hello Guys, I'm new so I hope that's the way things goes around here !大家好,我是新来的,所以我希望这就是这里的事情!

Like y'all know, iOS 13 introduced UI changes.众所周知,iOS 13 引入了 UI 更改。 We have an app in production and I recently woke up (maybe a little too late haha) and as I compiled and launch it on a freshly updated iOS 13 device, that's when I became aware there was some work to do !我们有一个正在生产的应用程序,我最近醒来(可能有点太晚了哈哈),当我在新更新的 iOS 13 设备上编译和启动它时,我意识到有一些工作要做! I handled the dark mode by not enabling it, I handled my modals but there is one thing I can't seem to make like iOS 12 and it's my Navigation Bar UI.我通过不启用它来处理暗模式,我处理了我的模态,但有一件事我似乎无法像 iOS 12 那样做,那就是我的导航栏 UI。

We use a custom back button image and after fighting during several hours, I finally succeeded but it's ok everytime except the first time.我们使用自定义的后退按钮图像,经过几个小时的战斗,我终于成功了,但除了第一次之外,每次都可以。 I will always have the default icon the first time, and then when I come back to the same controller, it's okay.我第一次总是使用默认图标,然后当我回到同一个控制器时,就可以了。

Here is a photo (at the beginning of the question) so you can understand and also my code !这是一张照片(在问题的开头)所以你可以理解我的代码! I know it's possible to use Appearance for specific VC with « whenContained » but I can't seem to figure it out cause it's all in navigation controller and I don't know how to differentiate them.我知道可以将 Appearance 用于带有 « whenContained » 的特定 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()
        }
    }

I'm basically become crazy here and I assume I'm missing something very obvious so if you guys have any hints or clues except apple documentation, feel free to share :)我基本上在这里变得疯狂,我想我遗漏了一些非常明显的东西,所以如果你们有任何提示或线索,除了苹果文档,请随时分享:)

Thanks in advance !提前致谢 !

In iOS 13 you can set up back button image and transition mask image only by the function在iOS 13中,您只能通过该功能设置后退按钮图像和过渡蒙版图像

func setBackIndicatorImage(UIImage?, transitionMaskImage: UIImage?)

And here is an example这是一个例子

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改导航栏上的后退按钮的图像? - How to change the image of back button on the navigation bar? 如何更改导航栏后退按钮的字体? - How can I change the font of the back button for my navigation bar? 如何更改“仅”导航右栏按钮图像? - How can I change 'JUST' the navigation right bar button image? 如何在导航栏中的按钮上设置约束? iOS 13 Swift 5(将圆形轮廓图像按钮添加到导航控制器) - How to set constraints on a button in a navigation bar? iOS 13 Swift 5 (Add circular profile image button to navigation controller) 如何删除 iOS 13 中出现的导航栏背景? - How I can remove navigation bar background with appearance in the iOS 13? iOS 5如何更改导航栏中后退按钮的颜色? - IOS 5 How to change the color of back button in a navigation bar? [ios]更改导航栏上后退按钮的标题 - [ios]change title of back button on navigation bar UINavigationBar 后退按钮标题无法使用 iOS 13 的导航栏外观移除 - UINavigationBar Back Button Title can't be removed using navigation bar appearance for iOS 13 后退按钮导航栏不改变图像 swift - back button navigation bar not change image swift 如何使用swiftui iOS 13从整个应用程序的导航栏中删除后退按钮 - How to remove back button from navigation bar in whole app using swiftui iOS 13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM