3

I wanted to change background color of the whole screen(including safe area) in SwiftUI.

Adding following code to outermost view works for all the views except if given view contains List

.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.red)

I wanted to add background color for the full screen containing List. I have tried multiple things like,

Adding ZStack with first element as Color.red, but not worked

Color.red with List as an overlay, but it still not worked.

Only thing working for me is converting List to ScrollView, but that I don't want.

Do anyone have any other solution to make that work, or Apple doesn't provide anyway to change background color for List?(should support atleast from iOS14 to latest)

3
  • 1
    You can’t until the next Xcode and iOS. You would have to make your own list using the other views such as a VStack Commented Jul 21, 2022 at 10:41
  • Yeah right. What I have found after starting swiftUI is, they made simple things complex and complex things to simple. Commented Jul 26, 2022 at 5:31
  • They are just trying to make coding more accessible. SwiftUI provides basic building blocks, it may slowly get to be as capable as UIKit but it is like using mega blocks instead of Lego’s. Commented Jul 26, 2022 at 16:04

1 Answer 1

2

Update: Xcode 14b4 - new beta, new fun.

There is new modifier on Xcode 14 for that (Xcode 14b4):

enter image description here

var body: some View {
    List {
        Text("Item 1")
        Text("Item 2")
        Text("Item 3")
    }
    //.scrollContentBackground(Color.blue) // << absent now !!
    .scrollContentBackground(.hidden) // Xcode 14b4+
    .background(Color.blue) // << also this needed !!
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know about this(I have already searched enough), but this is not backward compatible and still in beta. What to do for our current implementations? thats why I mentioned that, it must be iOS 14+ compatible.
@MehulThakkar Did you solve that? Got the same issue
@Tometoyou I haven't find proper solution yet, but the workaround is to use ScrollView instead of using List in this case. In swiftui both scrollview and list works almost similar except some inbuilt padding and rowseparator etc. lazyloading portion is handled by ForEach, so you can use scrollView when required instead of going for List.

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.