【发布时间】:2021-02-06 02:42:36
【问题描述】:
在这里,我使用了 SwiftUI 2.0 并管理 TabBar 徽章计数。参考文献
https://medium.com/flawless-app-stories/swiftui-tutorial-showing-badge-on-tab-bar-item-d71e4075b67a
在 Xcode 12.1 中,当键盘出现时,徽章会被向上推
当键盘出现时如何管理此徽章计数?
【问题讨论】:
在这里,我使用了 SwiftUI 2.0 并管理 TabBar 徽章计数。参考文献
https://medium.com/flawless-app-stories/swiftui-tutorial-showing-badge-on-tab-bar-item-d71e4075b67a
在 Xcode 12.1 中,当键盘出现时,徽章会被向上推
当键盘出现时如何管理此徽章计数?
【问题讨论】:
尝试将 .ignoresSafeArea(.keyboard) 添加到 GeometryReader 和/或包含 ZStack 的徽章。
您的视图正在调整大小以避免键盘,这是 iOS 14 中的新默认设置。使用新的 .ignoresSafeArea(.keyboard) 修饰符来禁用该行为。
【讨论】:
从 SwiftUI 3 开始,您可以使用 the .badge modifier 将徽章添加到您的标签项。这需要 iOS 15 或更高版本。
例子:
struct Tabs_Previews: PreviewProvider {
static var previews: some View {
TabView {
Text("String tab")
.tabItem {
Text("String")
Image(systemName: "text.quote")
}
.badge("hi")
.tag("string")
Text("Int tab")
.tabItem {
Text("Int")
Image(systemName: "number.circle")
}
.badge(123)
.tag("string")
}
}
}
结果:
您可以使用Int、String、Substring、LocalizedStringKey 或 Text。在我的测试中,徽章会忽略应用于 Text 的任何样式。
【讨论】: