【问题标题】:How to set content to Top in ScrollView?如何在 ScrollView 中将内容设置为 Top?
【发布时间】:2021-09-12 01:04:28
【问题描述】:

我正在尝试将滚动视图中的内容 Text("Test.....") 设置为顶部。不使用 .frame (maxHeight: 150) 等。我只需要在 Text ("123")) 下启动它,但没有任何结果。并且在各个方向滚动时应该可以工作。这只是一个示例,以了解如何从技术上进行操作。可以帮忙的人!

import SwiftUI

struct MainView: View {
var body: some View {
    NavigationView {
        content
    }
    .padding()
  }
}

private extension MainView {
var content: some View {
    VStack {
        ZStack(alignment: .topLeading) {
            ScrollView([.vertical, .horizontal]) {
                Text("TestTestTestTestTestTestTestTestTestTestTest")// set to top in content
                    .frame(height: 100)
                    .background(Color.green)
                    //.position(y: 0)
            }
            Text("123")
        }
        .background(Color.blue)
    }
  }
}

【问题讨论】:

  • Spacer() 不能帮你填满空间吗?

标签: ios scroll swiftui scrollview zstack


【解决方案1】:

您可以为此使用 .offset()

.offset(y: -230)

.offset(y: UIScreen.main.bounds.height * -0.28) // for responsive

【讨论】:

  • 您好!在我的应用程序中,内容可以有不同的大小,因此您需要自动计算并且不适合某个大小。到目前为止,答案并不合适。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 2013-05-06
  • 2012-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
相关资源
最近更新 更多