【问题标题】:SwiftUI - How to programatically scroll to top and show a large navigation title?SwiftUI - 如何以编程方式滚动到顶部并显示大导航标题?
【发布时间】: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")
      }
    }
  }
}

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

【问题讨论】:

    标签: ios swiftui


    【解决方案1】:

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

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

    【讨论】:

    • 很遗憾,这不起作用 - 大显示模式无法下推以显示自己。非常感谢您以任何方式回复。
    猜你喜欢
    • 2020-06-23
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 1970-01-01
    • 2016-02-12
    • 2020-03-23
    相关资源
    最近更新 更多