简体   繁体   English

SwiftUI:ScrollView 偏移滚动内容

[英]SwiftUI: ScrollView Offset Scroll Content

I'm trying to implement a ScrollView using SwiftUI that contains an HStack and ForEach() of calendar objects to scroll through back and forth to select a date to mimic below:我正在尝试使用包含日历对象的 HStack 和 ForEach() 的 SwiftUI 实现 ScrollView 来回滚动以选择要模仿的日期:

在此处输入图片说明

The array of dates would be say 30 days in the past of today's date and 30 days in the future.日期数组将是今天日期的过去 30 天和未来的 30 天。 When I initialize the ScrollView, it always starts on the 30th day in the past (Array index 0) but how could I offset the scroll of the ScrollView to where today's date is centered in the view?当我初始化 ScrollView 时,它总是从过去的第 30 天开始(数组索引 0),但是如何将 ScrollView 的滚动偏移到今天日期在视图中的中心位置? I've tried using .content.offset(x: offset) but it physically offsets the location of the scroll in the view, not the amount scrolled.我试过使用.content.offset(x: offset)但它物理偏移了视图中滚动的位置,而不是滚动的数量。

        ScrollView(.horizontal, showsIndicators: false) {

            HStack(alignment: .center, spacing: self.spacing) {

                ForEach(self.dateArray, id: \.self) { date in
                    CalendarObject(date: date)
                }
            }
        }
        .content.offset(x: -self.offset, y: 0)

In apple's example swiftUI Code, they just use padding of cell in ScrollView.在苹果的示例 swiftUI 代码中,他们只是在 ScrollView 中使用单元格的填充。 Maybe it is an answer from apple.也许这是来自苹果的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM