A horizontally scrollable Swift Chart writes an incorrect date to its chartScrollPosition(x:) binding when I tap the chart to stop its deceleration while the height of a view above the chart changes.
The binding receives a date near the beginning of the chart’s X-axis domain, even though the chart’s visible viewport remains near its previous position. Scrolling again causes the binding to update to a date matching the visible viewport.
Steps to reproduce:
Run the attached sample and open “Chart Header Height Reproduction”.
Leave “Keep header height fixed” turned off.
Swipe the chart horizontally across a boundary between a period with readings and a period without readings. The header above the chart changes height at this boundary.
While the chart is still decelerating, tap it to stop the scroll.
Observe the displayed scroll position and the [CHART_SCROLL_TRACE] console output.
Expected result:
The scroll-position binding continues to represent the chart’s visible position. Stopping deceleration and changing the height of a view above the chart should not cause the binding to receive a date near the beginning of the domain.
Actual result:
The binding receives a date near 1 January 2012, the beginning of the X-axis domain, while the chart’s viewport remains near its previous position in 2026. Scrolling again updates the binding to a date matching the viewport.
Additional observations:
The issue does not reproduce in my testing if I let the chart finish decelerating without tapping it.
The issue also stops reproducing when “Keep header height fixed” is enabled.
The call stack for the unexpected binding write includes Charts and SwiftUI geometry-update frames.
Changing the chart’s leading inset changes the unexpected date. When the viewport’s global leading X coordinate was 56 points, the incoming date corresponded to 56 points from the full plot’s domain start. When the leading X coordinate was 86 points, the incoming date corresponded to 86 points from the domain start. This suggests a possible coordinate-space issue during the geometry update, though I cannot verify the internal calculation.
Environment:
Device: iPhone 15, iPhone 17 pro (simulator)
iOS version and build: 27.0.1, 27.0
Xcode version and build: 27.0
The attached sample uses generated data.
struct ChartScrollHeightReproduction: View {
@StateObject private var model = ScrollHeightModel()
@State private var fixedHeaderHeight = false
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Text("Swipe the chart, then tap while it is decelerating. Repeat across periods with and without readings.")
.font(.caption)
Toggle("Keep header height fixed", isOn: $fixedHeaderHeight)
if fixedHeaderHeight {
ZStack(alignment: .leading) {
dataHeader.opacity(model.hasVisibleData ? 1 : 0)
emptyHeader.opacity(model.hasVisibleData ? 0 : 1)
}
} else if model.hasVisibleData {
dataHeader
} else {
emptyHeader
}
VStack(spacing: 12) {
Text("Scroll position: \(model.position.formatted(date: .abbreviated, time: .shortened))")
.font(.caption.monospaced())
.frame(maxWidth: .infinity, alignment: .leading)
Chart(model.visiblePoints) { point in
BarMark(
x: .value("Date", point.date),
yStart: .value("Minimum", point.value - 8),
yEnd: .value("Maximum", point.value + 8),
width: .fixed(8)
)
.foregroundStyle(model.selectedID == point.id ? .red : .pink)
.cornerRadius(4)
}
.chartXAxis {
AxisMarks(values: model.monthStarts) { value in
if let date = value.as(Date.self) {
AxisValueLabel {
Text(date.formatted(date: .abbreviated, time: .omitted))
.font(.caption2)
}
AxisGridLine()
}
}
}
.chartXScale(domain: model.domain)
.chartXVisibleDomain(length: 86_400)
.chartYScale(domain: 0...100)
.chartScrollableAxes(.horizontal)
.chartScrollPosition(x: Binding(
get: { model.position },
set: { model.receivePosition($0) }
))
.onTapGesture {
model.selectedID = model.visiblePoints.min {
abs($0.date.timeIntervalSince(model.position)) < abs($1.date.timeIntervalSince(model.position))
}?.id
}
.padding()
}
.frame(height: 250)
.background(Color.white)
}
.padding()
}
.navigationTitle("Chart Scroll Reproduction")
}
private var dataHeader: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Heart rate").font(.subheadline)
Text("Readings available").font(.title2.bold())
Text("Swipe into a period without readings").font(.caption)
}
.padding(.vertical, 8)
}
private var emptyHeader: some View {
Text("-- bpm").font(.title).padding(.vertical, 8)
}
}
private struct ScrollHeightPoint: Identifiable {
let id = UUID()
let date: Date
let value: Double
}
1
0
107