[英]How to horizontally align views with navigation bar items in SwiftUI
How do you get views to align with the navigation bar items?如何让视图与导航栏项目对齐? This can be done in a UIKit app via view.layoutMarginsGuide
.这可以通过view.layoutMarginsGuide
在 UIKit 应用程序中完成。
Here's an example:这是一个例子:
var body: some View {
NavigationView {
VStack {
Text("Lorem ipsum nunc fermentum euismod.")
.background(Color.gray)
.padding() //FIXME
.navigationBarTitle("Title")
.navigationBarItems(leading: Button("Hello"){}, trailing: Button("World"){})
Spacer()
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading) //make it fill the width
}
.navigationViewStyle(StackNavigationViewStyle())
}
This is not a full answer, and it only addresses a small part of your question, but:这不是一个完整的答案,它只解决了您问题的一小部分,但是:
Interestingly, on iPhone, the text actually is aligned if you take kerning into account.有趣的是,在 iPhone 上,如果您考虑字距调整,文本实际上是对齐的。 This is a good example:这是一个很好的例子:
Generated by this body:由该机构生成:
var body: some View {
NavigationView {
VStack(alignment: .leading) {
Text("Title.").background(Color.gray).padding(.leading)
Text("Hello.").background(Color.gray).padding(.leading)
Text("Lorem ipsum nunc fermentum euismod.").background(Color.gray).padding(.leading)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.navigationBarTitle("Title")
.navigationBarItems(leading: Button("Hello"){}, trailing: Button("World"){})
}
.navigationViewStyle(StackNavigationViewStyle())
}
If we zoom in, we can see the kerning in action:如果我们放大,我们可以看到字距调整:
It turns out that on iPhone––though apparently not on iPad––the text is actually aligned.事实证明,在 iPhone 上——尽管显然不是在 iPad 上——文本实际上是对齐的。 Notice that the H
s are aligned and that the T
s are aligned:请注意, H
是对齐的,而T
是对齐的:
This doesn't really answer your question because you still experience the issue on iPad.这并不能真正回答您的问题,因为您仍然会在 iPad 上遇到问题。 But it does at least narrow it down.但它至少缩小了范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.