简体   繁体   English

iOS 自定义导航栏后退按钮标题在点击时消失

[英]iOS Custom Navigation bar back button title disappears on tapping

Hello StackOverflow community,你好 StackOverflow 社区,

I have a rather curious case of a customized back button on my navigation controller's navigation bar disappearing on interaction.我有一个相当奇怪的案例,我的导航控制器导航栏上的自定义后退按钮在交互时消失。

Some additional information is that my code is not using Storyboard and I use UIKit - everything including the UI has been build programatically.一些额外的信息是我的代码没有使用 Storyboard 而我使用 UIKit - 包括 UI 在内的一切都是以编程方式构建的。

This is my set up:这是我的设置:

  • I start with creating a custom UINavigationController subclass to abstract away all of my navigation controller related customizations我首先创建一个自定义 UINavigationController 子类来抽象出我所有与导航控制器相关的自定义

  • 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)
}
  • The above customization, gives the following result which is what I desire with Gallery being the title of my previous screen上述定制,给出了以下结果,这是我想要的,画廊是我上一个屏幕的标题

UINavigationController 自定义后退按钮

  • When I tap towards the left most edge of the back button where the arrow is, the button works normally.当我点击箭头所在的后退按钮的最左侧边缘时,该按钮正常工作。 However, if I tap on the label section of the back button, where the Gallery text is, the text disappears and keeps the user on the same screen.但是,如果我点击后退按钮的标签部分(图库文本所在的位置),文本就会消失并使用户保持在同一屏幕上。

UINavigationController UINavigationBarButton 后退按钮消失

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 tried I have tried many suggestions on stack overflow such as:我试过什么我试过很多关于堆栈溢出的建议,例如:

  • making sure no line in my code hides the back button explicitly确保我的代码中没有一行明确隐藏后退按钮
  • Checking that previous screen's title to an empty string at any point随时检查前一个屏幕的标题为空字符串

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 Back Button not visible, but works UINavigationController 后退按钮不可见,但有效

UINavigationController's back button disappears? UINavigationController 的后退按钮消失了?

Navigation title disappears after use of custom back button 使用自定义后退按钮后导航标题消失

navigation title on previous screen disappear when back button pressed 按下后退按钮时,前一屏幕上的导航标题消失

Missing Navigation's or Back Button's Title When Push ViewControllers in Succession 连续推送 ViewController 时缺少导航或后退按钮的标题

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.所以经过大量的反复试验,这似乎对我有用。

I needed to even specify what the attributes are of the highlighted state.我什至需要指定highlighted状态的属性。

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.无论如何,如果其他人可能面临类似的情况,我会将这个答案放在这里。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM