简体   繁体   中英

Adding background color for full screen containing List/Tableview in swiftUI

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)

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

在此处输入图像描述

var body: some View {
    List {
        Text("Item 1")
        Text("Item 2")
        Text("Item 3")
    }
    .scrollContentBackground(Color.blue) // << here !!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM