ScrollView 剪切的阴影

inf*_*ero 10 ios swift swiftui

有没有办法可以将阴影应用于图像以同时显示在父视图之外?您可以在下面看到一个示例,其中我列出了 aScrollView和中的一些图像HStack。图像/艺术品应用了阴影,但这会被ScrollView.

截图供参考: 在此输入图像描述

ScrollView(.horizontal, showsIndicators: false) {
                                        HStack {
                                            ForEach(stationsFeed.items.sorted(by: { $0.updatedAt > $1.updatedAt }).prefix(15)) { item in

                                                NavigationLink(destination: StationDetailView(station: item)) {
                                                    VStack(alignment: .leading) {

                                                        if item.artwork != nil {
                                                            KFImage(self.artworkURLForStation(item)!)
                                                                .resizable()
                                                                .frame(width: 130, height: 130)
                                                                .scaledToFit()
                                                                .cornerRadius(6)
                                                                .shadow(radius: 5)
                                                        } else {
                                                            RoundedRectangle(cornerRadius: 6)
                                                                .background(Color.purple)
                                                                .shadow(radius: 5)
                                                        }

                                                        Text(item.name)
                                                            .font(.callout)
                                                            .foregroundColor(.primary)

                                                        Text(item.categories.first?.name ?? "N/A")
                                                            .font(.callout)
                                                            .foregroundColor(.secondary)
                                                    }
                                                    .frame(minWidth: 0, maxWidth: 130, alignment: .leading)
                                                }
                                                .buttonStyle(PlainButtonStyle())
                                            }
                                        }
                                    }
Run Code Online (Sandbox Code Playgroud)

Asp*_*eri 10

要使阴影不被剪切,请向 HStack 添加填充

演示

ScrollView(.horizontal, showsIndicators: false) {
   HStack {
      ForEach(stationsFeed.items.sorted(by: { $0.updatedAt > $1.updatedAt }).prefix(15)) { item in
       // ... other code
   }.padding()        // << here !!
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是解决方案,只是问题的解决方法 - 如果阴影半径 &gt;= 默认 SwiftUI 填充值,这不会掩盖问题。 (6认同)
  • 谢谢,这为我指明了正确的方向。但是,我需要向 ScrollView 添加 ``.padding(.horizo​​ntal, -20).padding(.vertical, -10)`` 才能使其看起来正确。有没有比硬编码填充更好的方法? (2认同)

Hol*_*ene 7

我已经找到了解决这个问题的一个不错的方法。

这是之前的图片

这是之后的图像

我向 ScrollView 和 ScrollView 的内容添加了 2 个填充,填充量相同,但其中一个具有负值,如下所示:

struct ScrollViewClippingProblem: View {
var body: some View {
    VStack {
        Text("Lorem Ipsum")
            .font(.title)
        
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(alignment: .top) {
                ForEach(0..<10, content: { item in
                    Text("\(item)")
                        .font(.title)
                        .padding()
                        .background(Circle().fill(.red))
                        .shadow(color: .blue, radius: 10, x: 15, y: 10)
                })
            }
            .padding(.vertical, 40) // <- add padding here to make the space for the shadow
        }
        .padding(.vertical, -40) // <- add NEGATIVE padding here of the same value to shrink the space you created with the padding above
        
        Text("Lorem Ipsum")
            .font(.title)
    }
    .background(.ultraThinMaterial)
}}
Run Code Online (Sandbox Code Playgroud)

我希望这对某人有帮助!:)