0
votes

I am trying to modify the navigation bar text attributes in SwiftUI (to add a text shadow) and have hit a wall trying to understand why I'm receiving run-time crashes in the simulator. There was another thread (ref: below) that was able to solve changing the font type using init() to modify the appearance, however trying to use init to modify the Text("") method to add a shadow results in a crash.
I've also tried extracting the Text("NavBarTitle") into its own method, then applying modifiers (no luck there). As seen in my code I've tried extracting the text into a variable results in a crash. Even just applying the modifiers directly causes a crash.

I'm not experienced enough in SwiftUI to call this a bug but it really feels like one.
Thanks for your help in advance!

import SwiftUI

struct ContentView: View {

    init() {
        UINavigationBar.appearance().largeTitleTextAttributes = [.shadow: 5]
    }

    let navigationBarText: Text = Text("Navigation Bar")

    var body: some View {
        NavigationView {
            VStack {
                Text("Hello, World!")
            }
            .navigationBarTitle(navigationBarText)

        }
    }
}

ref: https://stackoverflow.com/a/57632229/1514009

1

1 Answers

1
votes

It should be provided NSShadow object instead of number, as below

SwiftUI navigation bar shadow

init() {
    let shadow = NSShadow()
    shadow.shadowOffset = CGSize(width: 5, height: 2)
    UINavigationBar.appearance().largeTitleTextAttributes = [.shadow: shadow]
}