如何解决在iOS 14的SwiftUI中右对齐.bottomBar ToolbarItem
我想在.bottomBar
的{{1}}的{{1}}的{{1}}上添加一个“ compose”按钮。
添加一个.toolbar
只需几乎中心即可对齐该项目:
NavigationView
这将产生以下结果:
不是我所期望的。甚至更奇怪的是,它没有完全居中对齐,只有几个像素的距离。
那我该怎么办
-
右对齐?
-
居中对齐?
谢谢
解决方法
我遇到了同样的问题,这是我发现的(Xcode 12.0 Beta 6)
右对齐
一种方法是使用两个.bottomBar
项目。
.toolbar(content: {
ToolbarItem(placement: .bottomBar) {
Spacer()
}
ToolbarItem(placement: .bottomBar) {
Button(action: {}) {
Text("Add List")
}
}
})
更清洁的方法是将ToolbarItemGroup
与Spacer()
一起使用。
.toolbar(content: {
ToolbarItemGroup(placement: .bottomBar) {
Spacer()
Button(action: {}) {
Text("Add List")
}
}
})
居中
要使某个项目居中,可以使用.status
作为放置位置。
.toolbar(content: {
ToolbarItem(placement: .status) {
Button(action: {}) {
Text("Add List")
}
}
})
,
每个ToolbarItem都必须包含单个视图,因此只需将Spacer移到单独的工具栏项中
通过Xcode 12b3测试
.toolbar {
ToolbarItem(placement: .bottomBar) {
Spacer()
}
ToolbarItem(placement: .bottomBar) {
Button(action: { print("Pressed") }) {
Image(systemName: "plus.circle.fill")
.imageScale(.large)
.font(.title)
}
}
}
注意:将其居中删除带有分隔符的工具栏项
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。