繁体   English   中英

SwiftUI - 如何以编程方式滚动到顶部并显示较大的导航标题?

[英]SwiftUI - How to programatically scroll to top and show a large navigation title?

最近添加到 iOS 14 ScrollViewReader添加了一种以编程方式滚动到 SwiftUI 中的视图的方法。

问:有没有一种方法可以滚动到 ScrollView 中最顶层的元素并显示一个大的导航标题?

预期效果:滚动到顶部后的大导航标题

结果:滚动到顶部后的内嵌导航标题

代码片段供参考:

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")
      }
    }
  }
}

此代码滚动到按钮点击时的顶部矩形,但它不会显示默认的大导航标题。 我怎样才能做到这一点?

(我还没有测试过:)尝试:

  1. 声明 state var 以切换导航显示模式
  2. add.navigationBarTitle("Home", displayMode: showLargeDisplayMode? .large: .inline)
  3. 在 proxy.scrollTo("scrollID") 之后切换showLargeDisplayMode

暂无
暂无

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

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