我用的是View,它有一些按钮、一个阴影和一个后跟列表 问题是阴影在List中不可见,但在ScrollView中可见.

以下是我的代码截图:

struct ContentView: View {
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Button {
                } label: {
                    Text("Button 1")
                }
                .frame(maxWidth: .infinity)
                
                Divider()
                    .frame(height: 44)
                
                Button {
                } label: {
                    Text("Button 2")
                }
                .frame(maxWidth: .infinity)
            }
            .background(Color.white
                .shadow(color: Color.gray, radius: 10, x: 0, y: 0)
                .mask(Rectangle().padding(.bottom, -20))
            )

            List(0..<10) { index in
                Text("Scrollable list")
            }
            .listStyle(PlainListStyle())
        }
    }
}

我必须将间距设为0,这样当列表滚动时,没有空格将是可见的

我想用列表来实现查看阴影.

推荐答案

这是因为与ScrollView中的Text不同,列表行是不透明的-它们的背景不透明.

当你已经在列表的顶部时,如果你试图进一步向上滚动,你会看到阴影在列表的"后面".

这可以通过设置列表的zIndex来修复,这样它就在阴影后面.

List(0..<10) { index in
    Text("Scrollable list")
}
.listStyle(PlainListStyle())
.zIndex(-1) // or another number that is smaller than the zIndex of the shadow

此外,列表行背景可以由修改器listRowBackground设置,因此您只需执行以下操作:

List(0..<10) { index in
    Text("Scrollable list")
    .listRowBackground(Color.clear)
}

当然,还有一种方法是通过添加.padding(.top, someLength)来手动将列表向下移动一点.如果您不希望阴影在显示的第一行达到cover,这将是合适的.

Swift相关问答推荐

SwiftUI - NavigationLink(value:)和navigationDestination不工作

我正在try 通过代码设置节中页眉和页脚的高度,但它不起作用

允许视图在视图内更改

使用变量(而非固定)的字符串分量进行Swift正则表达式

SwiftUI路径平滑连接两条线

SwiftUI:列表背景为空列表时为黑色

解码 JSON 时 Swift Struct Decoder 初始化程序错误

是否有一个带有空初始值设定项的 Swift 类/ struct 的标准协议

deinitialize() 与 deallocate()

快速递归:函数与闭包

SwiftUI View .tint(_ Color) 方法不起作用

在 Swift 中为(递归)扩展提供默认实例参数

如何在 ZStack 中单独下移卡片?

在 Swift 中实现自定义异步序列

不要从 NumberFormatter 获取货币符号

在 SwiftUI 中,如何在 UIView 内或作为 UIView 使用 UIHostingController?

仅在 Swift 中创建 Setter

FCM 后台通知在 iOS 中不起作用

Swift 中惰性 var 的优势是什么

如何在 Swift 中重写 setter