Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ScrollView's background color in NavigationView in SwiftUI

Tags:

ios

swift

swiftui

I can't set a background color under ScrollView in SwiftUI. When I use .background(Color.red) the background is cut off so it doesn't go under navigation bar and scrolling seems to be broken. I tried a couple of solutions but each of them doesn't work. I have a simple view hierarchy

NavigationView {
    ScrollView([.vertical], showsIndicators: true) {
        VStack {
            ForEach(0...50, id: \.self) { _ in
                Text("Text text")
            }
        }
    }
    .navigationBarTitle("Title", displayMode: .large)
}
.navigationViewStyle(StackNavigationViewStyle())

it works as expected

enter image description here

now, I would like to add a background color, I tried the following solutions

1

NavigationView {
    ScrollView([.vertical], showsIndicators: true) {
        VStack {
            ForEach(0...50, id: \.self) { _ in
                Text("Text text")
            }
        }
    }
    .background(Color.red)
    .navigationBarTitle("Title", displayMode: .large)
}
.navigationViewStyle(StackNavigationViewStyle())

result

enter image description here

2

NavigationView {
    ZStack {
        Color.red
        ScrollView([.vertical], showsIndicators: true) {
            VStack {
                ForEach(0...50, id: \.self) { _ in
                    Text("Text text")
                }
            }
        }
        .navigationBarTitle("Title", displayMode: .large)
    }
}
.navigationViewStyle(StackNavigationViewStyle())

result

enter image description here

3

NavigationView {
    ZStack {
        Color.red.edgesIgnoringSafeArea([.all])
        ScrollView([.vertical], showsIndicators: true) {
            VStack {
                ForEach(0...50, id: \.self) { _ in
                    Text("Text text")
                }
            }
        }
        .navigationBarTitle("Title", displayMode: .large)
    }
}
.navigationViewStyle(StackNavigationViewStyle())

result

enter image description here

How to set up a background color under ScrollView packed in NavigationView?

// edit:

The animation below presents a desirable effect (it is made with UIKit).

enter image description here

like image 794
kampro Avatar asked Oct 23 '19 08:10

kampro


People also ask

How do I change the background color of a navigation bar in SwiftUI?

To change a navigation bar color in SwiftUI, you apply toolbarBackground modifier to the content view of NavigationStack . NavigationView is deprecated in iOS 16. toolbarBackground accepts two parameters. ShapeStyle : The style to display as the background of the bar.

How do I change the color of a list in SwiftUI?

There are two steps to change the SwiftUI list background color. Hide the standard background of the List. Set a new background color.

How do I make a background in SwiftUI?

SwiftUI doesn't have a dedicated modifier for displaying background colors or images, but instead lets us specify any kind of background view using its background() modifier. To be clear, you can use any view as your background – another text view if you wanted, for example.


1 Answers

After exhausting all possibilities, I landed with the below solution which works perfectly for what you want to achieve.

struct DemoView: View {
    
    init(){
        let navigationBarAppearance = UINavigationBarAppearance()
        navigationBarAppearance.backgroundColor = UIColor.red
        UIScrollView.appearance().backgroundColor = UIColor.red
    }
    
    var body: some View {

        NavigationView{
                ScrollView{
                    VStack{   
                        ForEach(0..<30, id: \.self){ _ in
                                Text("Text Text").foregroundColor(Color.black)
                        }
                    }.frame(maxWidth: .infinity)
                     .background(Color(UIColor.red))
            
            }.navigationBarTitle("Title")
        }  
    }
}

Hope this helps;)

like image 124
user2580 Avatar answered Oct 13 '22 12:10

user2580