2

When I try to use navigationBarTitleDisplayMode in my project targeting watchOS 6, I get this error:

'navigationBarTitleDisplayMode' is only available in application extensions for watchOS 8.0 or newer

How can I use it in earlier versions of watchOS? I know it won't have an effect there, because the style doesn't exist, I just want to circumvent the error.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223

1 Answers1

1

You can do it with a ViewBuilder extension. This will not make the effect appear on watchOS versions below 8.0, it's just for circumventing the issue of not being able to use the modifier.

extension View {
    @ViewBuilder
    func navBarTitleDisplayMode(_ mode: NavigationBarItem.TitleDisplayMode) -> some View {
        if #available(watchOSApplicationExtension 8.0, *) {
            self
                .navigationBarTitleDisplayMode(mode)
        } else {
            self
        }
    }
}

Usage:

someView
    .navigationBarTitle("WatchFunk") // Using this for watchOS 6 compatibility.
                                     // Use navigationTitle when targeting
                                     // watchOS 7 and above.
    .navBarTitleDisplayMode(.inline)
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223