ios - 仅在固定标题时自定义节标题,否则不固定(SwiftUI)

标签 ios swift swiftui header

我的问题很简单:是否有任何方法可以仅在固定时自定义节的标题,但如果未固定则保持原样?

我正在尝试将 .shadow 添加到部分的页眉中,我不希望它一直可见,但仅当开始向下滚动到父 ScrollView 中的页眉时(当页眉被固定时) .

我主要是在寻找一个纯粹的 SwiftUI 解决方案,但我也愿意讨论其他解决方案。 :)

struct WorkoutCardView: View {
    @Binding var workout: Workout
    @State var expandWorkout: Bool = false
    
    @Environment(\.colorScheme) var colorScheme
    
    var body: some View {
        LazyVStack(alignment: .leading, pinnedViews: .sectionHeaders) {
            Section {
                if expandWorkout {
                    WCExerciseSectionView(workout: $workout)
                }
            } header: {
                WCTitleSectionView(workout: $workout)
                    .background {
                        Color(uiColor: .systemBackground)
                        Color(uiColor: .systemFill)
                    }
                    .cornerRadius(10)
                    .shadow(color: colorScheme == .light ?
                            Color.black.opacity(expandWorkout ? 0.6 : 0) :
                                Color.white.opacity(expandWorkout ? 0.6 : 0), radius: 5, x: 0, y: 2)
                    .padding(.all, 2)
            }
        }
        .padding(.all, 8)
        .background {
            RoundedRectangle(cornerRadius: 10)
                .fill(Color(uiColor: .systemFill))
        }
        .padding(.all, 8)
        .onTapGesture {
            withAnimation {
                expandWorkout.toggle()
            }
        }
    }
}

Check current result here

最佳答案

一种可能的方法是基于动态检测标题相对于容器坐标空间的位置,如果它为零(即固定在顶部),则相应地更改样式。

使用 Xcode 13.4/iOS 15.5 测试

demo

这是主要部分:

                } header: {
                    HeaderView(value: "HEADER \(i + 1)", pinned: i == pinned)
                        .background(GeometryReader {
                            // detect current position of header
                            Color.clear.preference(key: ViewOffsetKey.self,
                                value: $0.frame(in: .named("area")).origin.y)
                        })
                }
                .onPreferenceChange(ViewOffsetKey.self) {
                    // verify if position is zero (pinned) in container coordinates
                    if $0 == 0 {
                        self.pinned = i
                    } else if self.pinned == i {
                        // clean-up if changed (might depend - for simplicity)
                        self.pinned = nil
                    }
                }
            }
        }
    }.clipped()
}.coordinateSpace(name: "area")    // << here !!

Test module on GitHub

关于ios - 仅在固定标题时自定义节标题,否则不固定(SwiftUI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72647382/

相关文章:

ios - 应用程序因 iOS 13.2 中的 CPU 使用而终止

Swift 将泛型转换为带有 nil 值的可选会导致 fatalError

ios - 如何使用 Swift UI 将忽略安全区域的 MapView 与我的其余内容对齐?

swift - 如何只取当前时间 (hh :mm:ss format) from Date() in Swift?

ios - Beta 测试,外部测试状态为非事件状态

ios - 如何将 Vuforia 应用程序作为模拟器版本提交给 Facebook 审核?

ios - 如何在单个 UI 按钮上启动和停止五彩纸屑 View ?

swift - <b></b> 之间的文本和 nilling<b></b> UILabel swift 之间的编程粗体字符串

ios - Swift - 使用来自异步方法的数据更新 View

ios - SwiftUI-无法将 View 拖到 ScrollView 之外