SwiftUI - 如何使导航栏标题可编辑(不改变任何其他行为)?
我真的很喜欢 SwiftUI 中导航栏标题的外观,而且我喜欢它出现在安全区域的正下方,但当你向下滚动时出现在工具栏的主要部分。我想知道如何完全复制这种外观和行为,但使其可由用户编辑(最有可能通过文本字段?)
我已经尝试过
.toolbar {
ToolbarItem(placement: .principal) {
TextField("Navigation Title", text: $mainTitle)
}
}
但这只是始终将标题放在工具栏中,而不是仅在滚动时轻微地。
有什么想法吗?
I really like the look of the navigation bar title in SwiftUI, and I like that it appears just below the safe area, but appears in the principal part of the toolbar when you scroll down. I'm wondering how to completely replicate this look and behavior but make it editable by the user (most likely through a textfield?)
I've tried
.toolbar {
ToolbarItem(placement: .principal) {
TextField("Navigation Title", text: $mainTitle)
}
}
But this simply places the title in the toolbar at all times, rather than only when you scroll slightly.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先我解释一下为什么你的代码不起作用:
当您开始滚动时,只有 navigationTitle 的大小会发生变化,而不是整个工具栏或其项目的大小。
但我想我有一个解决方案:
代码的作用是:它从第一个(在本例中)列表项中获取顶部 Y 位置。
然后它检查第一个列表项是否位于标题栏下方,并在必要时更改标题的字体大小。
我看到的唯一问题是小标题和大标题之间有一个相当粗糙的过渡,但我认为你可以弄清楚如何解决这个问题。
如果您对代码如何工作有更多疑问,请询问
我希望能解决您的问题。
我建议您观看 Paul Hudson 关于 Geometry Reader 的视频(他是一位出色的 YouTuber):https://youtu .be/kh9lnIYgW1E
我刚刚意识到视频标题中写着“OLD”,所以它可能已经过时了。
但他还有一些有关几何阅读器的其他视频。
只需搜索“保罗·哈德森几何读本”
First I explain why your code does not work:
Only the size of the navigationTitle changes when you start to scroll, not the size of the whole toolbar or its items.
But I think I have a solution:
What the code does is: It gets the top Y position from the first (in this example) list item.
Then it checks if the first list item is under the title bar and changes the font size of the title if necessary.
The only Problem I see is that there is a pretty rough transition between small and big title but I think you can figure out how to fix this.
If you have more questions how the code works just ask
I hope that solves your question.
And I would recommend to have a look at Paul Hudson’s video about the Geometry Reader (he’s a great YouTuber): https://youtu.be/kh9lnIYgW1E
I just realized that it says „OLD“ in the video title, so it may be outdated.
But he has some other videos about the Geometry Reader.
just search for „Paul Hudson Geometry Reader“