【发布时间】:2021-11-30 02:45:57
【问题描述】:
最近添加到 iOS 14 ScrollViewReader 添加了一种以编程方式滚动到 SwiftUI 中的视图的方法。
问:有没有办法在 ScrollView 中滚动越过最顶部的元素并显示一个大的导航标题?
预期效果: Large navigation title after scrolling to top
结果: Inline navigation title after scrolling to top
参考代码sn-p:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ScrollViewReader { proxy in
ScrollView {
Rectangle()
.fill(.yellow)
.aspectRatio(contentMode: .fit)
.id("scrollID")
Rectangle()
.fill(.blue)
.aspectRatio(contentMode: .fit)
Rectangle()
.fill(.green)
.aspectRatio(contentMode: .fit)
Rectangle()
.fill(.red)
.aspectRatio(contentMode: .fit)
Button("Scroll to top") {
withAnimation {
proxy.scrollTo("scrollID")
}
}
}
.navigationTitle("Navigation")
}
}
}
}
此代码滚动到按钮点击时的顶部矩形,但它不显示默认的大导航标题。我怎样才能做到这一点?
【问题讨论】: