SwiftUI 列表颜色背景 [英] SwiftUI List color background

查看:31
本文介绍了SwiftUI 列表颜色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我列出静态项目,我将无法更改视图的背景颜色.这是我的代码:

I can't change background color of a view if i List static items. this my code:

NavigationView {
    ZStack {
        Color("AppBackgroundColor").edgesIgnoringSafeArea(.all)
        List {
            Section(header: Text("Now in theaters")) {
                ScrollMovies(type: .currentMoviesInTheater)
            }
            Section(header: Text("Popular movies")) {
                ScrollMovies(type: .popularMovies)
            }
        }.listStyle(.grouped)
    }
}

推荐答案

所有 SwiftUI 的 List 都由 iOS 中的 UITableView 支持.所以你需要改变tableView的背景颜色.你让它clear,所以其他views 将在它下面可见:

All SwiftUI's Lists are backed by a UITableViewin iOS. so you need to change the background color of the tableView. You make it clear so other views will be visible underneath it:

struct ContentView: View {
    
    init(){
        UITableView.appearance().backgroundColor = .clear
    }
        
    var body: some View {
        Form {
            Section(header: Text("First Section")) {
                Text("First cell")
            }
            Section(header: Text("Second Section")) {
                Text("First cell")
            }
        }
        .background(Color.yellow)
    }
}

现在您可以使用任何背景(包括所有颜色)

Now you can use Any background (including all Colors) you want

注意那些顶部和底部的白色区域是安全区域,您可以使用 .edgesIgnoringSafeArea() 修饰符来摆脱

Note that those top and bottom white areas are the safe areas and you can use the .edgesIgnoringSafeArea() modifier to get rid of them.

还要注意 List 带有 .listStyle(GroupedListStyle()) 修饰符可以用一个简单的 Form 代替代码>.但请记住,SwiftUI 控件的行为取决于它们的封闭视图.

Also note that List with the .listStyle(GroupedListStyle()) modifier can be replaced by a simple Form. But keep in mind that SwiftUI controls behave differently depending on their enclosing view.

另外您可能希望将 UITableViewCell 的背景颜色设置为 clear 以及普通 tableviews

Also you may want to set the UITableViewCell's background color to clear as well for plain tableviews

这篇关于SwiftUI 列表颜色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆