3

I'm trying to change my View background colour to a specific color, however, whenever I add it using the basic Zstack way, it loses the navigation bar UI. (See pictures)

EDITED CODE

This method is not working for me:

var body: some View {
    ZStack{
        Color("Background")
            .edgesIgnoringSafeArea(.vertical)
        VStack {
            ScrollView {
                ZStack {

                    VStack {
                        HStack{
                            VStack (alignment: .leading) {
                                Text("")
                            }
                        }
                    }
                }
            }
            Text("")
        }
    }
}

Current UI with simple ZStack:

Current UI

Desired UI: Desired UI

How do I change my background color in SwiftUI without losing the navigation bar UI?

Simbeul
  • 441
  • 4
  • 11

1 Answers1

0

At this period of SwiftUI evolution it is possible only as workaround via UIKit

Here is a demo of possible approach (tested with Xcode 12.1 / iOS 14.1):

demo

var body: some View {
    NavigationView {
            VStack {
                ScrollView {
                    VStack {
                        ForEach(0..<50) {
                            Text("Item \($0)")
                        }
                    }
                }
                Text("Test").navigationTitle("Test")
                .background(UINavigationConfiguration { nc in
                     nc.topViewController?.view.backgroundColor = .yellow
                })
            }
    }
}

Note: the UINavigationConfiguration is taken from next my answer https://stackoverflow.com/a/65404368/12299030

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Sorry, I should have mentioned I have elements outside the Scrollview. I'll post an edit to my question, as well as a screenshot of the current UI with your suggestion. As you will see, the background change didn't work... – Simbeul Dec 28 '20 at 06:21