skip to Main Content

I have a ViewController featuring a large title. I want the back button to have a minimal display mode, but this causes the large title animation to become balky. This issue occurs when the view appears on the screen under the popped view controller.

Here is the code with the proper animation for the title (the title apears smoothly as if from the secound view controller nav bar)

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    navigationController?.navigationBar.prefersLargeTitles = true
    title = "Title"
    navigationItem.backButtonDisplayMode = .default
  }
}

enter image description here

And this is the problematic animation (the title apears as if it comes from the distance of the first view controller navigation bar).

 class ViewController: UIViewController {
      override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.navigationBar.prefersLargeTitles = true
        title = "Title"
        navigationItem.backButtonDisplayMode = .minimal
      }
    }

enter image description here

Is there something that can be done, so I’ll have a minimal backButtonDisplayMode, but the default animation?

2

Answers


  1. Although it’s quite difficult to understand what you’re after (and the fact that the two snippets provided are identical) I will try to treat this question as "I want to get the first result – but without a back title".

    A few things to note here:

    • minimal here just means "No back button title – just display the chevron"
    • Since the snippets are identical (probably by error) I assume that the first one is just without the minimal setting (because we can see the back title in the second VC).
    • The only difference in the two recordings is that in the first one (the one with the back title) there is a transition from the back button to the large title with an opacity change (fade). I assume that this fade is what you ultimately want.

    Now, the framework here notices that there is nothing to transition from (back button title) so it omits the opacity change.

    And there is nothing we can do about it… unless we are willing to work around it by exploiting what we already know.

    So we think… What if we drop the minimal and just set navigationItem.backButtonTitle = "" it should work, right? Well no (and this is quite familiar to developers with extensive UIKit experience) because the system treats empty strings as no button title present at all.

    Which leads us (you guessed it) to use a space for the back title:

    // Note that `backButtonDisplayMode` must not set up to `minimal`
    navigationItem.backButtonTitle = " "
    

    That ultimately produces:

    enter image description here

    Login or Signup to reply.
  2. I think what you want is to customize the transition animation in your navigation bar.

    Something like this:

    enter image description here

    source

    For this you can check this code in Github (its old but will do the trick)

    Or this newer code on Github swiftui-navigation-transitions

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search