SwiftUI - 为什么导航栏和我编写的第一个组件之间有空格?

发布于 2025-01-11 05:46:29 字数 896 浏览 3 评论 0原文

我有一个 SwiftUI 视图,其顶部如下所示:

在此处输入图像描述

其中“命名您的项目”是一个文本字段,顶部的按钮是导航视图工具栏的一部分。我将这个视图定义如下:

NavigationView {
  ZStack {
    Color("backgroundPrimary")
    .ignoresSafeArea()
        
     VStack(alignment: .center) {
       ZStack {
         ScrollView(showsIndicators: false) {
           TextField("Name your workout...", text: $mainTitle)
           .padding(.horizontal)
           .font(.custom("Helvetica Bold", size: 30))
    
            ...
        }
      }
    }

我希望 TextField 放置在靠近顶部的位置,就像 NavigationTitle 的位置一样,但相反,有一个我不知道其起源的间隙。所有堆栈都没有任何垂直填充。我尝试将 .padding(.top, -30) 添加到 TextField,但这会弄乱您可以点击 TextField 以调出键盘的区域。

注意:工具栏以蓝线结束,然后是间隙,然后是文本字段。

有谁知道如何将此 TextField 定位到更靠顶部的位置?

I have a SwiftUI view, the top of which looks like this:

enter image description here

where "name your item" is a TextField and the buttons at the top are part of a NavigationView toolbar. I've defined this view as follows:

NavigationView {
  ZStack {
    Color("backgroundPrimary")
    .ignoresSafeArea()
        
     VStack(alignment: .center) {
       ZStack {
         ScrollView(showsIndicators: false) {
           TextField("Name your workout...", text: $mainTitle)
           .padding(.horizontal)
           .font(.custom("Helvetica Bold", size: 30))
    
            ...
        }
      }
    }

I want the TextField to be positioned nearer to the top, much like the position of a NavigationTitle, but instead there's this gap I don't know the origin of. None of the stacks have any vertical padding. I've tried adding .padding(.top, -30) to the TextField but this messes up the area that you can tap on the TextField to bring up the keyboard.

Note: The toolbar ends at the blue line, then there's the gap, then there's the TextField.

Does anyone know how I can position this TextField further towards the top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

败给现实 2025-01-18 05:46:29

它仍然是为标题节省空间的NavigationBar。添加

    .navigationBarTitleDisplayMode(.inline)

到您的视图。

It is still the NavigationBar saving room for the title. Add

    .navigationBarTitleDisplayMode(.inline)

to your view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文