【问题标题】:How to manage Tab bar badge count in SwiftUI 2.0?如何在 SwiftUI 2.0 中管理标签栏徽章计数?
【发布时间】: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 tabbar


    【解决方案1】:

    尝试将 .ignoresSafeArea(.keyboard) 添加到 GeometryReader 和/或包含 ZStack 的徽章。

    您的视图正在调整大小以避免键盘,这是 iOS 14 中的新默认设置。使用新的 .ignoresSafeArea(.keyboard) 修饰符来禁用该行为。

    【讨论】:

      【解决方案2】:

      从 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")
              }
          }
      }
      

      结果:

      您可以使用IntStringSubstringLocalizedStringKeyText。在我的测试中,徽章会忽略应用于 Text 的任何样式。

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 1970-01-01
        • 2013-12-01
        • 2017-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        相关资源
        最近更新 更多