[英]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.