You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 17中ScrollViewProxy scrollTo动画消失:是Bug还是API变更?

问题解答

这是iOS 17中SwiftUI的API行为变更,并非Bug。

在iOS 16及更早版本,ScrollViewProxy.scrollTo()可以通过包裹在withAnimation中来实现滚动动画,但iOS 17调整了这个逻辑——官方为scrollTo方法新增了**animation参数**,专门用于控制滚动的动画效果,原有的withAnimation包裹方式不再触发动画。

修改后的代码如下:

import SwiftUI

struct ScrollTest: View {
    var body: some View {
        ScrollViewReader { proxy in
            List {
                Button("Scroll") {
                    proxy.scrollTo(15, anchor: .top, animation: .default)
                }
                
                ForEach(1..<50) { i in
                    Text("Item \(i)")
                        .id(i)
                }
            }
        }
    }
}

你可以根据需求替换.default为其他动画类型(比如.easeInOut(duration: 0.5))来定制滚动的动画效果。

内容的提问来源于stack exchange,提问作者wheatset

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 23:43:08