问题描述
你好 StackOverflow 社区,
Hello StackOverflow community,
我有一个相当奇怪的案例,我的导航控制器导航栏上的自定义后退按钮在交互时消失了.
I have a rather curious case of a customized back button on my navigation controller's navigation bar disappearing on interaction.
一些额外的信息是我的代码没有使用 Storyboard 而我使用的是 UIKit - 包括 UI 在内的一切都是以编程方式构建的.
Some additional information is that my code is not using Storyboard and I use UIKit - everything including the UI has been build programatically.
这是我的设置:
我首先创建一个自定义 UINavigationController 子类来抽象我所有与导航控制器相关的自定义
I start with creating a custom UINavigationController subclass to abstract away all of my navigation controller related customizations
在这个子类中,我调用了这个方法来自定义后退按钮的外观
Within this subclass, I have this method that get's called to customized the appearance of my back button
private func customizeBackButton() {
let backImage = UIImage(named: "BackButton")?.withRenderingMode(.alwaysOriginal)
navigationBar.backIndicatorImage = backImage
navigationBar.backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .normal)
}
- 上述定制,给出了以下结果,这是我想要的,画廊是我上一个屏幕的标题
- 当我点击箭头所在的后退按钮的最左侧边缘时,该按钮正常工作.但是,如果我点击后退按钮的标签部分(图库文本所在的位置),文本就会消失并使用户保持在同一屏幕上.
- 确保我的代码中没有一行明确隐藏后退按钮
- 随时检查前一屏幕的标题为空字符串
当我在标签消失后点击后退箭头时,它会工作并带我回到上一个屏幕,但是,为什么会发生这种消失?
When I tap on the back arrow after the label disappears, it works and takes me back to the previous screen, however, why would this disappearance occur ?
我尝试了什么我已经尝试了很多关于堆栈溢出的建议,例如:
What have I triedI have tried many suggestions on stack overflow such as:
我可以确认这些不是问题,因为我有一个有效的标题并且从不隐藏后退按钮.
I can confirm that these are not the issue as I have a valid title and never hide the back button.
以下是我在决定它们都不适用于我的案例之前检查过的一些资源,我在这里提出了问题:
Here are some resources I have checked before deciding that none of them work for my case and I asked the question on here:
UINavigationController 后退按钮不可见,但有效
UINavigationController 的后退按钮消失了?
缺少导航或后退按钮的标题何时连续推送 ViewControllers
关于为什么这个后退按钮文本消失的任何想法?
Any ideas as to why this back button text disappears ?
推荐答案
所以经过大量的反复试验,这似乎对我有用.
So after a lot of trial and error, this is what seemed to have worked for me.
我什至需要指定 highlighted
状态的属性.
I needed to even specify what the attributes are of the highlighted
state.
UIBarButtonItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .highlighted)
此后,文本再也没有消失,一切都按预期进行.
After this, the text never disappeared and everything worked as intended.
我暂时不将此标记为答案,因为也许有人有更好的权利"方法来做到这一点.
I am not marking this as the answer for now as perhaps someone has a better "right" way to do this.
无论如何,如果其他人可能面临类似的情况,我会将这个答案放在这里.
In any case I am putting this answer here if anyone else might be facing a similar scenario.
这篇关于iOS 自定义导航栏后退按钮标题在点击时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!