Swift and SwiftUI tutorials for Swift Developers

How to change the background color in a List view in SwiftUI

Starting with iOS 16, you can customize the color of the scrollable area of ​​a list view. Simply set the background color using the background modifier and attach the scrollContentBackground modifier to the List view. By setting the parameter to .hidden, you can easily change the scroll view’s background to another color as in this code example:

List {
            
            ForEach(restaurants.indices, id: \.self) { index in
                
                if(0...1).contains(index) {
                    
                    FullImageRow(restaurant: restaurants[index])
                }else{
                    
                    BasicImageRow(restaurant: restaurants[index])
                    
                }
                
            }
            
            .listRowSeparator(.hidden)
            
        }
        .background(.blue)
        .scrollContentBackground(.hidden)

The result in XCode would be the following:

How to add an image as a background

We can also set an image as a background, using the following Swift code with the SwiftUI framework:

.background{
            
            Image("homei")
                .resizable()
                .scaledToFill()
                .clipped()
                .ignoresSafeArea()
            
            
        }
        .scrollContentBackground(.hidden)

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Article

How to hide the SwiftUI List Row separators

Next Article

How to use NavigationStack in SwiftUI

Related Posts