1

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 Answer 1

1

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]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.