6

On iOS 15, when navigating to a view controller that has a transparent navigation bar, the animation to the transparent bar isn't working as expected.

However, if you navigate back to the view controller with a transparent navigation bar, the animation works as expected.

This is how I've set up my view controllers:

rootVC

let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = UIColor.red

self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance

firstVC

let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()

self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance

secondVC

let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = UIColor.yellow

self.navigationController?.navigationBar.standardAppearance = appearance
self.navigationController?.navigationBar.scrollEdgeAppearance = self.navigationController?.navigationBar.standardAppearance

Notice in the following example how smooth the transition is from secondVC -> firstVC but not from rootVC -> firstVC:

video demonstration

Example project: https://github.com/karlingen/NavigationTest

Any ideas why it's behaving like this?

karlingen
  • 13,800
  • 5
  • 43
  • 74
  • Isn't it kind of unsupported to change the navigation bar transparency as part of the navigation? It's supposed to just sit there consistently. – matt Sep 29 '21 at 14:43
  • Also could this be related to https://stackoverflow.com/questions/41842359/black-smudge-under-navigation-controller-when-transitioning ? – matt Sep 29 '21 at 14:45
  • @matt why would it be unsupported? The docs doesn't say anything about that, unless I've missed something? Also, this works fine on iOS < 15 – karlingen Sep 29 '21 at 15:02
  • Well that's why I said "kind of" :) – matt Sep 29 '21 at 15:20
  • Transition between transparent and opaque navigation bars always been choppy on iOS. I would recommend to use third-party lib for that. I had good experience with this one: https://github.com/MoZhouqi/KMNavigationBarTransition – ManWithBear Sep 29 '21 at 18:11
  • @ManWithBear it has worked fine up until iOS 15 – karlingen Sep 30 '21 at 06:09
  • 1
    @matt Fixed! See solution below – karlingen Oct 04 '21 at 08:42

1 Answers1

16

I got the following reply from Apple:

You should get better behavior using per-item customization, that is setting these properties on their view controller’s UINavigationItem instead of on the UINavigationBar itself. This also frees you from the strict timing necessary for the viewWillAppear: approach to work – as long as your customizations are applied before the view controller is pushed, you should get a good transition. viewDidLoad is generally a good place to do these customizations when using the per-item customization support.

So we should be using UINavigationItem instead. Using the following code fixed it for me:

# firstVC.swift    
override func viewDidLoad() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithTransparentBackground()
    
    self.navigationItem.standardAppearance = appearance
    self.navigationItem.scrollEdgeAppearance = appearance
}
karlingen
  • 13,800
  • 5
  • 43
  • 74