首页 > 解决方案 > 为什么 List 会改变 SwiftUI 中的背景颜色?

问题描述

到目前为止,基本上我所拥有的是 TabView 内的 NavigationViews。我目前正在处理的问题如下:我通过初始化程序将颜色应用于 TabView 和 NavigationView。现在,当我将任何视图(例如 Text())添加到 HomeView 时,一切都很好,但现在我想添加一个列表,它会以某种方式自动将背景颜色和所有内容更改为系统默认颜色(暗/亮)。我有将 listRowBackground 更改为特定颜色的想法,但是当将 List 向下拖动一点时,您会再次看到它后面的默认系统背景...

选项卡视图:

struct MainView: View {

init() {
    UITabBar.appearance().barTintColor = Theme.current.background
    UITabBar.appearance().unselectedItemTintColor = Theme.current.backgroundSecondary

    UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)
    UINavigationBar.appearance().shadowImage = UIImage()
    UINavigationBar.appearance().tintColor = Theme.current.backgroundPrimary
}

var body: some View {

    TabView {
        HomeNavigationView().tabItem {
            Image("HomeIcon")
        }.tag(0)

        //Left out the other tabs
        ...

    }.edgesIgnoringSafeArea(.top)
     .accentColor(Color(Theme.current.brand))
}

}

主页导航查看:

struct HomeNavigationView: View {

var body: some View {

    NavigationView {
        HomeView()
    }
}

}

HomeView 添加简单 Text() 时:

struct HomeView: View {

var body: some View {

    ZStack {
        Color.red.edgesIgnoringSafeArea(.all)

        Text("Test Text")
    }

    //Left out the NavigationBar icons
    ...

}

}

添加列表时的 HomeView:

struct HomeView: View {

var body: some View {

    ZStack {
        Color.red.edgesIgnoringSafeArea(.all)

        List {
            Text("first row")
            Text("second row")
            Text("third row")
        }
    }

    //Left out the NavigationBar icons
    ...

}

}

一切正常时的截图问题截图

标签: iosswiftxcodeuikitswiftui

解决方案


推荐阅读