【问题标题】:Transparent Background for SwiftUI Lists -- Behavior Change in iOS14SwiftUI 列表的透明背景——iOS14 中的行为变化
【发布时间】:2020-09-17 22:38:03
【问题描述】:

我有一个带有背景的 SwiftUI 列表。在 iOS 13 中,通过在 init() 中设置 UITableView 属性,我成功地使列表透明,以便背景显示出来。在 iOS 14 中,行为发生了变化。下面的代码 sn-p 显示了初始化设置。我已确认此提取的 sn-p 在 iOS 13 中按预期工作(通过列表显示背景),但在 iOS 14 中,列表中填充的行会阻塞背景,就好像背景是白色且不清晰一样。

还有其他人看过吗?是否有另一种方法可以使列表透明并适用于 iOS 13 和 14?

struct RecorderList: View {
    init(){
        UITableView.appearance().backgroundColor = .clear
        UITableViewCell.appearance().backgroundColor = .clear
        UINavigationBar.appearance().largeTitleTextAttributes = [
            .foregroundColor: UIColor.purple,
            .font: UIFont(name:"Papyrus", size: 40) ?? UIFont.systemFont(ofSize:40)]
    }
    
    var body: some View {
        
        NavigationView {
            ZStack (alignment: .top){
                Image("background")
                    .resizable()
                    .scaledToFit()
                List {
                    Text("One")
                        .font(.title)
                        .background(Color.clear)
                    Text("Two")
                        .font(.title)
                        .background(Color.clear)
                    Text("Three")
                        .font(.title)
                        .background(Color.clear)
                    Text("Four")
                        .font(.title)
                        .background(Color.clear)
                    Text("Five")
                        .font(.title)
                        .background(Color.clear)

                    }
                }
                .navigationBarTitle("Recorders")
            }
        }
}

【问题讨论】:

  • 有同样的问题。看来,UITableViewCell.appearance().backgroundColor = ... 在 iOS 14 中没有效果——我称之为错误。模拟器 iOS 14.5 中的行为仍然相同。 listRowBackground(..) 也不起作用。

标签: swiftui ios14


【解决方案1】:

你可以使用listRowBackground:

var body: some View {
    NavigationView {
        ZStack(alignment: .top) {
            Image("background")
                .resizable()
                .scaledToFit()
            List {
                Group {
                    Text("One")
                    Text("Two")
                    Text("Three")
                    Text("Four")
                    Text("Five")
                }
                .font(.title)
                .listRowBackground(Color.clear)
            }
        }
        .navigationBarTitle("Recorders")
    }
}

另外,通过将视图放入Group,您可以分别对每个视图应用修饰符。

【讨论】:

  • 谢谢 pawello2222——这很好。然而,有趣的是,第一次尝试并没有。它适用于我的精简示例,就像您上面的修改一样,但在我的实际应用程序中,文本视图在 NavigationLink 块内实例化,然后在 ForEach 块内实例化,只需在 Text 上添加 .listRowBackground 不起作用。在我在 ForEach 循环内的 NavigationLink 内的单个 Text 视图周围添加了一个 Group 块,然后将 .listRowBackground() 放在该 Group 上之后,它确实起作用了。再次感谢!
  • 应用listRowBackground(Color.clear) 没有成功。为了使单元格背景和表格视图背景清晰,您另外需要设置UITableViewCell.appearance().backgroundColor = .clear 和UITableView.appearance().backgroundColor = .clear - 然后设置列表背景:.background(myBackColor.edgesIgnoringSafeArea(.all))。
  • 从 Xcode 12.5 开始,这对我不起作用。
【解决方案2】:

由于某种原因,listRowBackgound 修饰符对我不起作用。我的短期目标是

.listRowInsets(EdgeInsets()) // hack for ios14
.background(Color.black) // hack for ios14

然后调整填充。我希望其他人有更好的主意。

【讨论】:

  • 这正是我所需要的,谢谢!
  • 该修饰符似乎只适用于 VStacks。当我将它应用到列表中的 VStack 时,它对我有用{}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-27
  • 1970-01-01
  • 2020-04-08
  • 1970-01-01
  • 1970-01-01
  • 2022-08-12
  • 1970-01-01
相关资源
最近更新 更多