0

Is there a way to get notified when the .navigationBarTitleDisplayMode (which is set to .automatic) of a view changes from .large to .inline or vice versa?

Something like an @Environment object similar to

@Environment(\.locale) var locale: Locale

Depending on the displayMode, I'd like to show a different title.
Something along the lines of:

@Environment(\.navigationBarTitleDisplayMode) var mode: NavigationBarTitleDisplayMode

...

.navigationBarTitle(mode == .large ? "I am the large title" : "Inline title")
.navigationBarTitleDisplayMode(.automatic)
Peanutsmasher
  • 220
  • 3
  • 13
  • can't do with SwiftUI. You would have to dig into UIKit > https://developer.apple.com/documentation/uikit/uinavigationbar/3198027-scrolledgeappearance – ChrisR Mar 20 '22 at 19:49

1 Answers1

0

This can be done by creating your own parameter.

@State private var mode: NavigationBarItem.TitleDisplayMode = .large
(...)
.navigationBarTitle(mode == .large ? "I am the large title" : "Inline title")
.navigationBarTitleDisplayMode(mode)

You can pass this var into the environment and have a global indicator.

Mark
  • 16,906
  • 20
  • 84
  • 117